0

Why do I always get this error for my code: Invalid literal for int() with base 10:’’

I’m trying to make it so that the current player guess is none and I will input a question later.

num1 = 10

answer1 = int("")

totalnum = num1 + answer1

answer1 = int(input("enter a number: "))

Traceback (most recent call last):
  File "<string>", line 7, in <module>
ValueError: invalid literal for int() with base 10: ''
> 
  • 1
    `""` is not a number, hence `int("")` fails. You should remove that line and move the `totalnum` line to the end of your script – ksbg Jun 06 '22 at 04:29
  • You don't need to initialise variables in that manner in Python. `int('')` tries to cast an empty string to a number. – Freddy Mcloughlan Jun 06 '22 at 04:34

2 Answers2

0

I think you have to remove the line answer1 = int("") from the above code.

num1 = 10

answer1 = int(input("enter a number: "))

totalnum = num1 + answer1
Metalgear
  • 3,391
  • 1
  • 7
  • 16
0

You can't convert an empty string to an integer, as you are trying to do with answer1 = int(""). You can either set answer1 to 0 as a default, or get the input before calculating totalnum.

Either:

num1 = 10

answer1 = 0

totalnum = num1 + answer1

answer1 = int(input("enter a number: "))

or

num1 = 10

answer1 = int(input("enter a number: "))

totalnum = num1 + answer1