-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_number.py
More file actions
70 lines (51 loc) · 2.05 KB
/
guess_number.py
File metadata and controls
70 lines (51 loc) · 2.05 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# import random library
import random
def guess(x):
print()
x = input('Enter a guess range of positive number: ')
# check if x is number or else throw error.
try:
x = int(x)
except:
print('Invalid! please enter positive range number.')
# secret number
random_num = random.randint(1,x)
# user guess
guess = 0
count = 5
while guess != random_num:
print()
guess = input(f'Guess a number between 1 and {x} or enter any character to exit the program: ')
try:
guess = int(guess)
except:
print()
print('You just entered character:',guess,' to exit the program!')
break
# check remaining chance
if count > 1:
print('Count Down! you remain with:', count,'chances.')
if guess == random_num:
print('Awesome! you won this time', guess, '=', random_num)
elif guess > random_num:
print('Incorrect! Hint: your guess is bigger than secret number by:', (guess - random_num))
else:
print('Incorrect! Hint: your guess is smaller than secret number by:',(guess-random_num))
elif count == 1:
print()
print('Count Down! you remain with:', count,'chance.')
if guess == random_num:
print('Awesome! you won this time', guess, '=', random_num)
elif guess > random_num:
print('Incorrect! Hint: your guess is bigger than secret number by:', (guess - random_num))
else:
print('Incorrect! Hint: your guess is smaller than secret number by:',(guess-random_num))
else:
print()
print('Count Down! you remain with:', count,'chance.')
print('Sorry! you have used up all your chances of guessing.')
print()
break
# count down
count = count - 1
guess(1000)