-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise9.py
34 lines (26 loc) · 998 Bytes
/
exercise9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Generate a random number between 1 and 9 (including 1 and 9).
# Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.
# Extras:
# Keep the game going until the user types “exit”
# Keep track of how many guesses the user has taken, and when the game ends, print this out.
import random
ran = random.randint(1, 9)
count = 0
stop = False
while(not stop):
num = input("Guess a number between 1 and 9, including 9, and see if it matches the random number: ")
count +=1
if int(num) == int(ran):
print(("Congratulations! You typed {} and the random number is {}.".format(num,ran)))
print("It took you {} guess(es).".format(count))
break
elif int(num) <= int(ran):
print("You guessed too low")
else:
print("You guessed to high")
answer = input("Do you wish to exit? Y/N. ")
if answer == "Y":
print("Game over.")
stop = True
else:
stop = False