Practice lists and tuples

Download Report

Transcript Practice lists and tuples

Week 6 : Sequences
( lists, strings, and tuples )
Today’s topic
 Fibonacci number
 Task1
 Memento game
 Task2-1
 Task2-2
Task 1 : Fibonacci number
 For your information, refer this definition of Fibonacci number.
 http://en.wikipedia.org/wiki/Fibonacci_number
 Using while-loop, you should fill a list with these Fibonacci numbers
which are less than 1000.
 Printed result of the list
>>> [evaluate fibonacci.py]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>>
 We recommend you to start with two initial values, 0 and 1.
Memento game
 Today we’ll implement a Memory test game, “Memento”
Python shell
### Welcome to the Python Memento
game!!! ###
1st try. You got 0 pairs.
Input the first card number : 0
Input the second card number : 4
Correct!
2nd try. You got 1 pairs.
Input the first card number : 10
Input the second card number : 19
Wrong..........
3rd try. You got 1 pairs.
Input the first card number : 8
Input the second card number : 19
Correct!
4th try. You got 2 pairs.
Input the first card number : 10
Input the second card number : 14
Correct!
……. Game goes on ………
The flow of Memento game
 There are 24 number pads.
 First, show all pictures for a while then flip them over
 The user have to input 2 card numbers.
>>> [evaluate memento.py]
Welcome to the Python Memento
 The screen shows two pictures you indicated ###
game!!! ###
1st try. You got 0 pairs.
 If the card numbers(indices) indicate the
Input the first card number :
same picture,
then show “Correct!” message and add
2 numbers to correct number list, and
show all the picture pairs which you have
founded.
 If not, show “Wrong……” message and
return the screens before.
 The game finishes when all pairs are
founded.
Three lists in Memento game
A list for managing corrected numbers
correct_list
0
4
8
•••
Two lists for visualization
cards
num_pads
....
Suwon.jpg
Sungeun.jpg
Yeongjae.jpg
....
Task 2-1 : Implementation - Function
 Implement two functions:
1. is_valid(num1,num2)
 Check if any of two numbers exists in the current correct list,
the two are same number, or they are valid numbers in the
given range.
 Return Boolean value according to the result.
2. check(num1,num2)
 At first, visualize the screen including the two cards (num1th
card and num2th card).
 If two pictures of the two cards are same, put the two
numbers into the correct list.
 If not, re-visualize the original screen.
 Return Boolean value according to the result.
Task 2-2 : Implementation - Others
1. Shuffle the “cards” list.
 Using “random” module.
2. Modify the condition for visualization in print cards().
3. Modify the condition of main while-loop.
 Your program should be terminated if you find all pairs.
4. Print the number of tries using the ordinal number.
>>> [evaluate memento.py]
### Welcome to the Python Memento game!!! ###
1st try. You got 0 pairs.
2nd try. You got 0 pairs.
3rd try. You got 0 pairs.
….
Rock and Roll!