File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ # Generate a random num
2+ import random
3+
4+ # Specifying the range bt taking input
5+ print ('Please enter the range of numbers -' )
6+ min = int (input ('Minimum : ' ))
7+ max = int (input ('Maximum : ' ))
8+ num = random .randint (min ,max )
9+
10+ # attempts
11+ attempts = 5
12+ guessed = 0
13+
14+ # loop
15+ while True :
16+ # take input guess
17+ try :
18+ print (f'You have only { attempts } attempts!' )
19+ guess = int (input (f'Guess the number between { min } and { max } : ' ))
20+
21+ # if num < guess : too low
22+ if guess < num :
23+ print ('Too low!' )
24+ guessed += 1
25+ attempts -= 1
26+ if attempts == 0 :
27+ print ('Attempts Over!' )
28+ print ('Sorry! Try Next Time!' )
29+ print (f'The number was { num } ' )
30+ break
31+
32+ # if num > guess : too high
33+ elif guess > num :
34+ print ('Too high!' )
35+ guessed += 1
36+ attempts -= 1
37+ if attempts == 0 :
38+ print ('Attempts Over!' )
39+ print ('Sorry! Try Next Time!' )
40+ print (f'The number was { num } .' )
41+ break
42+
43+ # else num == guess : well done
44+ else :
45+ guessed += 1
46+ print (f'Congratulations! You guessed the number in { guessed } attempts!' )
47+ break
48+
49+ # if invalid num : error
50+ except ValueError :
51+ print ('Please enter a valid number' )
You can’t perform that action at this time.
0 commit comments