Skip to content

Commit ad70dd2

Browse files
Create pg.98.guess#.py
1 parent 5f84848 commit ad70dd2

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pg.98.guess#.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is a guess the number game.
2+
import random
3+
secretNumber = random.randint(1, 20)
4+
print('I am thinking of a number between 1 and 20.')
5+
# Ask the player to guess 6 times.
6+
for guessesTaken in range(1, 7):
7+
print('Take a guess.')
8+
guess = int(input())
9+
10+
if guess < secretNumber:
11+
print('Your guess is too low.')
12+
elif guess > secretNumber:
13+
print('Your guess is too high.')
14+
else:
15+
break # This condition is the correct guess!
16+
17+
if guess == secretNumber:
18+
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
19+
else:
20+
print('Nope. The number I was thinking of was ' + str(secretNumber))
21+
22+
'''
23+
Output example:
24+
I am thinking of a number between 1 and 20.
25+
Take a guess.
26+
10
27+
Your guess is too low.
28+
Take a guess.
29+
15
30+
Your guess is too low.
31+
Take a guess.
32+
17
33+
Your guess is too high.
34+
Take a guess.
35+
16
36+
Good job! You guessed my number in 4 guesses!
37+
'''

0 commit comments

Comments
 (0)