Apparently this is a duplicate question but I did search before posting it and couldn't find anything that helped. I'm not aware of having asked it more than once.
First let me say that I'm a noob at this and so my jargon is not quite up to scratch so please be gentle if you feel that I haven't described my problem properly. Thank you.
I have written this script and it works perfectly:
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print ("You have six tries to guess the secret number between 1 and 99")
guess = int(input("What's first your guess?"))
while tries < 5 and guess != secret:
if guess < secret:
print ("Too low.")
tries = tries + 1
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
elif guess > secret:
print ("too high")
tries = tries + 1
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
if guess == secret:
print ("you got it!")
else:
print ('No more guesses. The secret number was', secret)
................................................................................ However when I define a function as follows:
def ordinal_guess():
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
and call the function as follows:
while tries < 5 and guess != secret:
if guess < secret:
print ("Too low.")
tries = tries + 1
ordinal_guess()
elif guess > secret:
print ("too high")
tries = tries + 1
ordinal_guess()
The script no longer wants to play nice, telling me the guess entered is always either too low or too high. see below:
You have six tries to guess the secret number between 1 and 99 What's first your guess?99 too high What's your second guess?1 too high What's your third guess?3 too high What's your fourth guess?5 too high What's your fifth guess?85 too high What's your sixth guess?50 No more guesses. The secret number was 81
clearly some of those numbers weren't too high.
Likewise here: You have six tries to guess the secret number between 1 and 99 What's first your guess?50 Too low. What's your second guess?99 Too low. What's your third guess?80 Too low. What's your fourth guess?70 Too low. What's your fifth guess?65 Too low. What's your sixth guess?53 No more guesses. The secret number was 75
What am I doing wrong?