Guess the Number is a classic game for beginners to practice basic programming techniques. In this game, the computer thinks of a random number between 1 and 100. The player has 10 chances to guess the number. After each guess, the computer tells the player if it was too high or too low.
When you run guess.py, the output will look like this:
Guess the Number, by Al Sweigart [email protected]
I am thinking of a number between 1 and 100.
You have 10 guesses left. Take a guess.
> 50
Your guess is too high.
You have 9 guesses left. Take a guess.
> 25
Your guess is too low.
--snip--
You have 5 guesses left. Take a guess.
> 42
Yay! You guessed my number!
Guess the Number uses several basic programming concepts: loops, if-else statements, functions, method calls, and random numbers. Python’s random
module generates pseudorandom numbers—numbers that look random but are technically predictable. Pseudorandom numbers are easier for computers to generate than truly random numbers, and they’re considered “random enough” for applications such as video games and some scientific simulations.
Python’s random
module produces pseudorandom numbers from a seed value, and each stream of pseudorandom numbers generated from the same seed will be the same. For example, enter the following into the interactive shell:
>>> import random
>>> random.seed(42)
>>> random.randint(1, 10); random.randint(1, 10); random.randint(1, 10)
2
1
5
If you restart the interactive shell and run this code again, it produces the same pseudorandom numbers: 2
, 1
, 5
. The video game Minecraft generates its pseudorandom virtual worlds from a starting seed value, which is why different players can re-create the same world by using the same seed.
1. """Guess the Number, by Al Sweigart [email protected]
2. Try to guess the secret number based on hints.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: tiny, beginner, game"""
5.
6. import random
7.
8.
9. def askForGuess():
10. while True:
11. guess = input('> ') # Enter the guess.
12.
13. if guess.isdecimal():
14. return int(guess) # Convert string guess to an integer.
15. print('Please enter a number between 1 and 100.')
16.
17.
18. print('Guess the Number, by Al Sweigart [email protected]')
19. print()
20. secretNumber = random.randint(1, 100) # Select a random number.
21. print('I am thinking of a number between 1 and 100.')
22.
23. for i in range(10): # Give the player 10 guesses.
24. print('You have {} guesses left. Take a guess.'.format(10 - i))
25.
26. guess = askForGuess()
27. if guess == secretNumber:
28. break # Break out of the for loop if the guess is correct.
29.
30. # Offer a hint:
31. if guess < secretNumber:
32. print('Your guess is too low.')
33. if guess > secretNumber:
34. print('Your guess is too high.')
35.
36. # Reveal the results:
37. if guess == secretNumber:
38. print('Yay! You guessed my number!')
39. else:
40. print('Game over. The number I was thinking of was', secretNumber)
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
input('> ')
on line 11 to input(secretNumber)
?return int(guess)
on line 14 to return guess
?random.randint(1, 100)
on line 20 to random.randint(1, 1)
?format(10 - i)
on line 24 to format(i)
?guess == secretNumber
on line 37 to guess = secretNumber
?