In my code for guessing game, even when the guess is 1, it returns that the guess is too high. Cant figure it out. Thanks!
2 Answers
Essentially you should not be using raw_input as this will get you a string and not an integer. Try using input
Please see this question for more details: Python read input as integers
7 Comments
Stephen
Important to note that raw_input is no longer available as of Python 3.0
Jae Song
i tried that too and it wont get rid of the issue which it always outputs guess is too high even when i input '1' and I'm running 2.7
Stephen
I just ran the code with the change suggested: int(input('Enter a number between 1 and 100: ')); and also: print str(guessNum) + " is too low". The code worked fine
Stephen
Make sure to change the raw_input in all 3 places
Jae Song
I tried the str(guessNum), now it only returns that it is too low instead of too high even when I tried 99
|
Your guessNum is string. Need to make it to int when ever you expect a user to input an integer. For example:
guessNum = int(raw_input('Enter a number between 1 and 100: '))
3 Comments
Jae Song
Traceback (most recent call last): File "C:\Users\Jae\Desktop\Scripting\q1", line 23, in <module> main() File "C:\Users\Jae\Desktop\Scripting\q1", line 12, in main print guessNum + " is too low" TypeError: unsupported operand type(s) for +: 'int' and 'str'
Marcin
@JeffCrackalack You cant add integers to strings. to do so, need to convert int to string or you string formating. Example of the first case:
str(guessNum) + " is too high, try again:" and second: "{} is too high, try again:".format(guessNum)Jae Song
can you please examine my comment to the answer(Chuckie) below. I really don't understand why its not printing correctly. Thank you so much
guessCounteris actually a running list with theguessNumsappended. So, add a+ len(guessCounter)at end of print statement. Also, it wouldn't be average but total.