1.) For the while response not in range(low, high) loop, even when the number entered is correctly within range the loop still goes through for some reason.
2.) After that when you re-enter your number again, the "lower.../higher..." condition statements will go through, but the loop will end after that.
I'd like to know how I'm putting together loops in the function, especially where I place return response.
- Code: Select all
# Guess My Number
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
# set the initial values
low = 1
high = 100
the_number = random.randint(1, 100)
def ask_number(guess, low, high):
"""Ask for a number within a range."""
response = None
# Loop whether the number is not within range
while response not in range(low, high):
response = int(input("\nNot Valid. Please take another guess: "))
# Loop whether the number is
while guess != the_number:
if guess > the_number:
elif guess == the_number:
print("You guessed it! The number was", the_number)
ask_number(int(input("\nTake a guess: ")),low, high)
input("\n\nPress the enter key to exit.")