Download Lesson 12: Random numbers in guessing game and silly

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Python (programming language) wikipedia , lookup

Video game programmer wikipedia , lookup

Transcript
IEC 2015 - Python Programming
Week 12 - random() continued
Guessing game and silly words list (final)
Lesson plan:
In this class, we asked the students to revisit their programs from the previous classes and
add a randomization component to it. We walked them through their old programs where
they had the user/programmer picked numbers. With some prompting, some were able to
understand why we would using random numbers and where we would need to place the
relevant commands.
The final programs looked something like this:
Guessing game: Initially, the programmer picked a number and the user had to guess it.
With randomization, neither programmer nor user could know what the original number
picked by Python.
import random
playAgain = ‘y’
while playAgain == 'y':
number = random.randint(1,10)
print("I'm thinking of a number from 1 to 10. Enter your guess.")
NumberOfGuesses = 4
while NumberOfGuesses > 0:
guess = input()
guess = int(guess)
if guess == number:
print("That's right!")
break
if guess < number:
print("Your guess is too low.")
if guess > number:
print("Your guess is too high.")
IEC 2015 - Python Programming
Week 12 - random() continued
NumberOfGuesses = NumberOfGuesses - 1
print("Game over!")
print("Play again? (y/n)")
playAgain = input()
Silly words list: Originally, the program asked the user to pick 2 numbers, one for an
adjective and one for a noun and pull up words with the corresponding index in the
dictionary. Now with randomization, the program automatically picks two random words
and places them in the sentence.
import random
adjective = {
1: 'smelly',
2: 'pretty',
3: 'green',
4: 'funny',
5: 'annoying',
6: 'sweet',
7: 'nice',
8: 'dirty',
9: 'clean',
10:'scary'
}
#noun
noun = {
1: 'pants',
2: 'shoes',
IEC 2015 - Python Programming
Week 12 - random() continued
3: 'trees',
4: 'stories',
5: 'birds',
6: 'gummi bears',
7: 'games',
8: 'dishes',
9: 'mugs',
10: 'jokes'
}
print("Do you want to play this game? (Type yes or no)")
playAgain = input()
while playAgain == 'yes' or playAgain == 'y':
anum = random.randint(1, 10)
nnum = random.randint(1,10)
print('These ' + noun[nnum] + ' are ' + adjective[anum] + '!!!')
print('')
print('Do you want to play again? (yes or no)')
playAgain = input()