Cannot figure out problem with random.randint(x,y)
Question:
I’m trying to write a program where you think of a number and the computer tries to guess it by going higher and lower but it refuses to work. I’ve narrowed the problem down to line 6 or 7 by trimming bits of the program in and out but I have no idea what’s wrong with those lines in order to fix it.
import random
print("Think of a number between 0 - 100nType:n'Higher' if the number is too lown'Lower' if the number is too highn'Correct' if the number is correct")
minRange = 0
maxRange = 100
while True:
comGuess = random.randint(minRange,maxRange)
print("Is it " + comGuess + "?")
answer = input("")
if answer == "Higher" or answer == "higher" or answer == "h":
minRange = comGuess
elif answer == "Lower" or answer == "lower" or answer == "l":
maxRange = comGuess
elif answer == "Correct" or answer == "correct" or answer == "c":
input("Yay I won!")
break
else:
print("I don't understand")
As far as I can tell it should say a number, you provide a response and it guesses again and again with ever narrowing parameters until it gets the correct answer. I’m still learning Python but the lack of error message is really stumping me, the program just opens and instantly closes. I have the input on the correct answer so the program doesn’t instantly close upon a correct answer
Answers:
on line 7, change print("Is it " + comGuess + "?")
to print("Is it " + str(comGuess) + "?")
. This works because you’re trying to combine a string and an int (a number) in a print function, and that won’t work, so you have to convert the int to a str first
I’m trying to write a program where you think of a number and the computer tries to guess it by going higher and lower but it refuses to work. I’ve narrowed the problem down to line 6 or 7 by trimming bits of the program in and out but I have no idea what’s wrong with those lines in order to fix it.
import random
print("Think of a number between 0 - 100nType:n'Higher' if the number is too lown'Lower' if the number is too highn'Correct' if the number is correct")
minRange = 0
maxRange = 100
while True:
comGuess = random.randint(minRange,maxRange)
print("Is it " + comGuess + "?")
answer = input("")
if answer == "Higher" or answer == "higher" or answer == "h":
minRange = comGuess
elif answer == "Lower" or answer == "lower" or answer == "l":
maxRange = comGuess
elif answer == "Correct" or answer == "correct" or answer == "c":
input("Yay I won!")
break
else:
print("I don't understand")
As far as I can tell it should say a number, you provide a response and it guesses again and again with ever narrowing parameters until it gets the correct answer. I’m still learning Python but the lack of error message is really stumping me, the program just opens and instantly closes. I have the input on the correct answer so the program doesn’t instantly close upon a correct answer
on line 7, change print("Is it " + comGuess + "?")
to print("Is it " + str(comGuess) + "?")
. This works because you’re trying to combine a string and an int (a number) in a print function, and that won’t work, so you have to convert the int to a str first