I just started out coding last month and decided to test myself with a Random Number Guesser (The user inputs a number between 1 and 10 and the code tries to guess it), but while trying to debug input errors I converted my integer into a string and don't know how to convert it back. My input currently looks like this:
def UserNum(prompt):
while True:
try:
value = int(input(prompt))
except ValueError:
print("Sorry, I didn't understand that.")
continue
if value < low:
print("That's not between " + str(low) + " and " + str(high) + "!")
elif value > high:
print("That's not between " + str(low) + " and " + str(high) + "!")
else:
break
UserNum(f"Please enter a number between {str(low)} and {str(high)}!\n")
but when 'RNGuesser' gets the prompt correct it acts as though it was incorrect, leading me to believe my prompt isn't an integer anymore. The rest of the code looks like this:
RNGuess = random.randint(low, high)
print(f"I guess {str(RNGuess)}")
while UserNum != RNGuess and guessing:
if guess_count <= guess_limit:
guess_count += 1
response = input("Was my guess correct, higher or lower?:\n")
if response == lower:
RNGuess = random.randint(int(RNGuess), high)
print("I guess " + str(RNGuess))
elif response == higher:
RNGuess = random.randint(low, int(RNGuess))
print("I guess " + str(RNGuess))
elif response == correct:
print("Really? I don't think I was")
else:
print("Invalid response")
else:
print("I give up, you win!")
guessing = False
while UserNum == RNGuess and guessing:
response = input("Was my guess correct, higher or lower?:\n")
if response == correct:
print("Yay! I win!")
guessing = False
elif response == higher:
print("Are you sure? I think I'm right")
elif response == lower:
print("Are you sure? I think I'm right")
else:
print("Invalid response")
any help would be greatly appreciated, Thanks!
Aucun commentaire:
Enregistrer un commentaire