0

I'm a beginner trying to improve my Python skills. I've found a very similar question to mine but since the source code was different, it did no benefit. Here is the link for the question: ValueError: invalid literal for int() with base 10: '' Anyways, here is my code:

correct_answer= 41
guess = int(input("Guess the number"))

if int(input()) == correct_answer:
print ("You found my number!")

if int (input()) >= correct_answer:
print ("My number is lower.")

if int (input()) <= correct_answer:
print ("My number is higher.")

else:
print ("You didn't write any numbers!")

Here, I wanted to write a simple guessing game. The number computer has in mind is 41, and the user has to guess the number after "Guess the number". If the input of a user is greater than 41, the program says "My number is lower" and if the input is smaller than 41, the program says "My number is greater." When I run this code, I get the "ValueError: invalid literal for int() with base 10: ''

Thank you in advance for helping me solve this problem :)

Sobhan
  • 17
  • 1
  • 8

2 Answers2

1

The problem is that you are calling input() again and again. Everytime you do this, the program expects input and the user has to retype their guess in order for the program to work as intended. Replace all instances of int(input()) (with an empty prompt) with guess, and you'll see more reasonable behavior. E.g.

if guess == correct_answer:
    print ("You found my number!")
alexis
  • 48,685
  • 16
  • 101
  • 161
0

Try this -

correct_answer= 41
guess = raw_input("Guess the number")
try:
    guess = int(guess)
except ValueError:
    print "Wrong value entered"

if guess == correct_answer:
    print ("You found my number!")
elif....

Let me know if that helps

When you take the input and someone enters an alphabet or something which is not an integer, and you try to typecast it, python will throw a ValueError.

To catch such cases, we use try except block will catch it. More about python try except in python

Couple of pointers about your code -

if int(input()) == correct_answer:

here, you are just calling input() function and your program will not work, use variable guess, this is the variable you have put your input into.

and

if int(input()) == correct_answer:
print ("You found my number!")

if int (input()) >= correct_answer: 
print ("My number is lower.")

if int (input()) <= correct_answer:
print ("My number is higher.")

Use python's if elif elif else instead.

Also, since you have typecasted to integer already, you don't need to do that with every if condition, this would suffice -

if input > correct_answer and you don't need >= or <=.

>= means greater or equal, but you have handled the equal case in first condition, if it's equal, it'll be caught there, similar with less than or equal

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • 1
    You'd have no way of knowing this, but "try this" is discouraged in answers on this site: It is a signpost for code that _might_ work, but is presented without explanation -- in this case, you gave a new solution but didn't spell out _why_ the OP's code is wrong. – alexis Jul 13 '18 at 11:33
  • Editing that, so that OP can understand what went wrong. – Sushant Jul 13 '18 at 11:44
  • Thanks for your guidance people. @ThatBird your answer was mostly correct but my program says "raw_input is not defined", therefore I just wrote "input " instead. Adding "except ValueError:" seems to have solved most of my problems, but this time the program runs with this error: "TypeError: '>' not supported between instances of 'builtin_function_or_method' and 'int'. " What's the problem this time? – Sobhan Jul 13 '18 at 19:54
  • See the edited answer, let me know if it solves the problem. If it doesn't, do post the line at which you're getting the error – Sushant Jul 13 '18 at 21:19