-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guessing Game.py
54 lines (44 loc) · 2.37 KB
/
Guessing Game.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
51
52
53
54
import random
def play_game(): #Define the Game function
while True: #Verify the user enters an integer for lower bound
lowerBound = input('Welcome to the Number Guessing Game! Please specify the lower bound of the random number: ') #User enters lower bound
try:
lowerBound = int(lowerBound) #Attempt to convert user inputted string to integer
except:
print('Please use numeric digits.') #Tell user to re-enter a numerical number
continue
break
while True: #Verify the user enters an integer for upper bound
upperBound = input('Great! Now set the upper bound: ') #User enters upper bound
try:
upperBound = int(upperBound) #Attempt to convert user inputted string to integer
except:
print('Please use numeric digits.') #Tell user to re-enter a numerical number
continue
if upperBound <= lowerBound: #Verify upper bound is greater than lower bound
print('The upper bound must be greater than the lower bound.')
continue
else:
break
number = random.randint(lowerBound,upperBound) #Randomize the number to be guessed
print('The number is set, attempt to guess it!')
guesses = 1 #Initialize the guesses variable at 1
while True: #Setting up guess loop
guessesString = str(guesses) #Convert the integer 'guesses' to a string for formatting
guess = int(input('Guess: ')) #User guesses a number
if guess > number: #User guessed too high
guesses += 1 #Increment total guesses
print('Your guess was too high! Try again.')
print(f'Total Guesses: {guessesString}') #Display total guesses
elif guess < number: #User guessed too low
guesses += 1 #Increment total guesses
print('Your guess was too low! Try again.')
print(f'Total Guesses: {guessesString}') #Display total guesses
else: #User guessed correctly!
guesses += 1 #Increment total guesses
print(f'You got it in {guessesString}' + ' Guesses!') #Display final guesses
break
while True: #Calling the game function
play_game()
if input('Play again? (y/n): ').lower() != 'y': #Ask user to play again or leave game
break