-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guessing_game_2.py
50 lines (43 loc) · 1.72 KB
/
number_guessing_game_2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
- Create a new random number guessing program.\
- All the previous conditions are the same plus:\
– Have the user keep guessing until the get the number correct.\
– Count the number of guesses.\
– If the number of guesses is 1, print "Excellent"\
– If the number of guesses 2-19 print "OK"\
– If the number of guesses is 20 print "Bad."
'''
import random
print("Guess Random Number Game")
gen_rand = random.randint(0,20)
while True:
try:
user_input = int(input("Guess a number between 0 and 20: \n"))
break
except:
print('Exception occured')
continue
count = 1
while gen_rand != user_input:
try:
if user_input in range(1,20): ###range() includes start value, excludes stop values
if gen_rand > user_input:
print("Your guess is less than the random number, guess bigger number \n")
user_input = int(input("Guess again a number between 0 and 20: \n"))
count += 1
elif gen_rand < user_input:
print("Your guess is higher than the random number, guess smaller number \n")
user_input = int(input("Guess again a number between 0 and 20: \n"))
count += 1
else:
print("Your guess is out of range")
user_input = int(input("Guess again a number between 0 and 20: \n"))
count += 1
except Exception as ex:
print(ex)
if count == 1:
print("Excellent! You guessed the correct number in the first attempt!")
elif 2<= count <= 19:
print(f'OK, You guessed the correct number in the {count} attempt!')
else:
print(f'Bad, You exhausted all the chances!')