Reputation: 83
I am a beginner of Python and using PyCharm for an exercise.
After inputting the following code:
promotion_percentage = int(input('Enter a integer from 1 to 100: '))
An error is then produced:
ValueError: invalid literal for int() with base 10: ''
I read quite a lot of Q&A in the forum. But their cases look more complicated than mine.
Where the error comes from?
Upvotes: 1
Views: 1534
Reputation: 4572
You're trying to convert something which isn't an integer. I can reproduce the problem when I type in any alpha character.
An easier way to see what is going on would be to make the input it's own variable and step through the debugger.
Try something like this and see what get's printed before the assert.
var = input('Enter a integer from 1 to 100: ')
print(repr(var), type(var))
assert var.isalnum(), "var should be only numbers"
promotion_percentage = int(var)
print(promotion_percentage, type(promotion_percentage))
Notice that when I type in only numbers, your code works.
Enter a integer from 1 to 100: 234
'234' <class 'str'>
234 <class 'int'>
but when I give it a non-integer...
Enter a integer from 1 to 100: abc
'abc' <class 'str'>
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: 'abc'
To explain the error you are specifically getting, your input is an empty string. This happens when you hit enter without typing anything. In which case I would follow the advice from this post.
var = input('Enter a integer from 1 to 100: ')
print(repr(var), type(var))
assert var.isalnum(), "var should be only numbers"
if not var:
# ask them again?
# exit script?
# set a default value?
Upvotes: 1