Issue converting to string to integer.
Chris Angelico
rosuav at gmail.com
Sun Jun 2 22:08:07 EDT 2013
On Mon, Jun 3, 2013 at 11:52 AM, Dan Sommers <dan at tombstonezero.net> wrote:
> On Sun, 02 Jun 2013 18:12:33 -0700, Fdama wrote:
>
>> I combined the int conversion and the input on the same line, rather
>> than to have two different statements. But got an error message:
>
>> Traceback (most recent call last):
>> File "C:\Users\Faisal\Documents\python\pizza_slicer.py", line 23, in <module>
>> start=int(input("\nStart: "))
>> ValueError: invalid literal for int() with base 10: ''
>>
>> Could someone tell me why I got this error message?
>
> The difference is what used to happen in between the input and the
> conversion. In the first version, the "if" statement prevents the
> conversion from happening when there is no input. In the second
> version, though, python tries to do the conversion with no input, and
> fails.
You could combine them in, as long as you're okay with blank input and
input of '0' being folded to the same:
start = int(input("\nStart: ") or 0)
ChrisA
More information about the Python-list
mailing list