Game Development Reference
In-Depth Information
guess.py
This code can be downloaded from http://inventwithpython.com/guess.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. # This is a guess the number game.
2. import random
3.
4. guessesTaken = 0
5.
6. print('Hello! What is your name?')
7. myName = input()
8.
9. number = random.randint(1, 20)
10. print('Well, ' + myName + ', I am thinking of a number
between 1 and 20.')
11.
12. while guessesTaken < 6:
13. print('Take a guess.') # There are four spaces in
front of print.
14. guess = input()
15. guess = int(guess)
16.
17. guessesTaken = guessesTaken + 1
18.
19. if guess < number:
20. print('Your guess is too low.') # There are eight
spaces in front of print.
21.
22. if guess > number:
23. print('Your guess is too high.')
24.
25. if guess == number:
26. break
27.
28. if guess == number:
29. guessesTaken = str(guessesTaken)
30. print('Good job, ' + myName + '! You guessed my
number in ' + guessesTaken + ' guesses!')
31.
32. if guess != number:
33. number = str(number)
34. print('Nope. The number I was thinking of was ' +
number)
Even though we are entering our source code into a new file editor window, we can
return to the shell to enter individual instructions in order to see what they do. The
interactive shell is very good for experimenting with different instructions when we are not
running a program. You can return to the interactive shell by clicking on its window or on
its taskbar button. In Windows or Mac OS X, the taskbar or dock is on the bottom of the
screen. On Linux the taskbar may be located along the top of the screen.
Search WWH ::




Custom Search