File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import random
2+
3+ def guessing_game ():
4+ print ("Welcome to the Number Guessing Game!" )
5+
6+ upper_bound = int (input ("Write the upper bound of the range: " ))
7+
8+ print (f"I'm thinking of a number between 1 and { upper_bound } ." )
9+
10+ number_to_guess = random .randint (1 , upper_bound )
11+ attempts = 0
12+ guessed_correctly = False
13+
14+ while not guessed_correctly :
15+ try :
16+ guess = int (input ("Make a guess: " ))
17+ attempts += 1
18+
19+ if guess < number_to_guess :
20+ print ("Too low." )
21+ elif guess > number_to_guess :
22+ print ("Too high." )
23+ else :
24+ guessed_correctly = True
25+ print (f"Congratulations! You've guessed the number { number_to_guess } in { attempts } attempts." )
26+ except ValueError :
27+ print ("Please enter a valid integer." )
28+
29+ if __name__ == "__main__" :
30+ guessing_game ()
You can’t perform that action at this time.
0 commit comments