Skip to content

Commit

Permalink
Merge pull request #696 from mmakrin/patch-3
Browse files Browse the repository at this point in the history
Update guess_number.py
  • Loading branch information
Mrinank-Bhowmick authored Feb 10, 2024
2 parents 62d242f + 62eb8f7 commit cdc894e
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions projects/Guess Number/guess_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,43 @@
larger_number = 10
upper_limit = 10
lower_limit = 1
flag= True



# function to prompt user for input. will continue to ask user for proper int if invalid num passed
def enter_and_verification(lower_limit, upper_limit):
user_guess = int(input(f"\nEnter number between {lower_limit} to {upper_limit}: "))
while user_guess > upper_limit or user_guess < lower_limit:
if user_guess > upper_limit:
user_guess = int(
input(
f"\nYour guess exceeds the upper range. Lower your guess and try again.\nEnter number between {lower_limit} to {upper_limit}: "
)
)
if user_guess < lower_limit:
user_guess = int(
input(
f"\nYour guess exceeds the lower range. Increase your guess and try again.\nEnter number between {lower_limit} to {upper_limit}: "
)
)
return user_guess

while True:
try:
user_guess = int(input(f"\nEnter number between {lower_limit} to {upper_limit}: "))
while user_guess > upper_limit or user_guess < lower_limit:
if user_guess > upper_limit:
user_guess = int(input(
f"\nYour guess exceeds the upper range. Lower your guess and try again.\nEnter number between {lower_limit} to {upper_limit}: "
))
if user_guess < lower_limit:
user_guess = int(input(
f"\nYour guess exceeds the lower range. Increase your guess and try again.\nEnter number between {lower_limit} to {upper_limit}: "
))
return user_guess
except ValueError:
print("Invalid input. Please enter a valid integer.")

# function to handle checking user input against random number and upper/lower bounds
def guess(num, user_guess):
def guess(num, user_guess,num_of_guesses):
upper_limit = 10
lower_limit = 1
while num != user_guess:
if num > user_guess:
print(f"\nNumber is higher than {user_guess}")
lower_limit = user_guess
user_guess = enter_and_verification(lower_limit + 1, upper_limit)
num_of_guesses = num_of_guesses + 1
num_of_guesses=num_of_guesses+1
elif num < user_guess:
print(f"\nNumber is lower than {user_guess}")
upper_limit = user_guess
user_guess = enter_and_verification(lower_limit, upper_limit - 1)
num_of_guesses = num_of_guesses + 1
num_of_guesses=num_of_guesses+1
else:
print()
print(f"\nCongrats! You've guessed the correct number! It was {num}.\n")
Expand All @@ -49,9 +50,9 @@ def guess(num, user_guess):

# while loop to prompt user to play intially, then continue to play or not
while True:
play_y_n = input("Welcome to Number Guesser. If you'd like to play, press 'Y': ")
play_y_n = input("Welcome to Number Guesser. If you'd like to play, press 'Y' or press 'X' if you want to exit: ")
if play_y_n.lower() == "y":
num_of_guesses = 0
num_of_guesses=0
num = random.randint(smaller_number, larger_number)
user_guess = enter_and_verification(lower_limit, upper_limit)
guess(num, user_guess, num_of_guesses)
Expand Down

0 comments on commit cdc894e

Please sign in to comment.