0

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!

1
  • Aside, your last print line seems to be incomplete. I believe you want to show number of guesses but your guessCounter is actually a running list with the guessNums appended. So, add a + len(guessCounter) at end of print statement. Also, it wouldn't be average but total. Commented Mar 6, 2015 at 2:45

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Important to note that raw_input is no longer available as of Python 3.0
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
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
Make sure to change the raw_input in all 3 places
I tried the str(guessNum), now it only returns that it is too low instead of too high even when I tried 99
|
0

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

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'
@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)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.