Issue converting to string to integer.

F

Fdama

Hi,

I was following an exercise in a book when I edited the code and came across something I did not get. Here is the relevant part of the code that works:

start=None #initialise
while start !="":
start=input("\nStart: ")

if start:
start=int(start)
finish=int(input("Finish: "))

print("word[",start,":",finish,"] is", word[start:finish])

I then changed the code to this:

start=None #initialise
while start !="":
start=int(input("\nStart: "))

if start:

finish=int(input("Finish: "))

print("word[",start,":",finish,"] is", word[start:finish])

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?

Thanks
 
D

Dan Stromberg

Hi,

I was following an exercise in a book when I edited the code and came
across something I did not get. Here is the relevant part of the code that
works:

start=None #initialise
while start !="":
start=input("\nStart: ")

if start:
start=int(start)
finish=int(input("Finish: "))

print("word[",start,":",finish,"] is", word[start:finish])

I then changed the code to this:

start=None #initialise
while start !="":
start=int(input("\nStart: "))

if start:

finish=int(input("Finish: "))

print("word[",start,":",finish,"] is", word[start:finish])

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: ''

Converting an empty string to base 10 doesn't fly, so if you hit a blank
line on the input(), you get a bad conversion. In the first version,
you're only converting to base 10 if the string is non-empty. In the
second, you're attempting to convert irrespective.

BTW, I'm partial to:

if not start:
continue

....but that's a stylistic thing.
 
A

alex23

I combined the int conversion and the input on the same line, rather thanto 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?

When you were presented the 'Start' prompt, you hit Enter, which
causes `input` to return an empty string. As it's passed directly
into `int`, which requires a valid string-representing-a-number, you
get the ValueError traceback, which shows you the failing input it
received: '' This doesn't happen in your original code, because your
condition `if start:` will fail on an empty string, and not try to
turn it into an int.

You don't want to wrap your `input` with an `int`. You want to test
the return result from `input` to ensure it can be coerced:

start = input("\nStart: "))
if start and start.isdigit():
start=int(start)
...
else:
start='' # set start to empty string so the while loop repeats

Here we check that all of the characters in the string `start` are
actually numbers before coercing. This is known as the "Look Before
You Leap" (LBYL) approach. Another approach is to catch the ValueError
exception:

start = input("\nStart: "))
try:
start = int(start)
except ValueError:
start = ''
if start:
....

This is known as "Easier to for Ask Forgiveness than
Permission" (EAFP). They both have their advantages. try/excepts tends
to be quicker if few exceptions are called, while an if/else will test
every time, although at this point it's not something you need to
overly concern yourself with. Go with whichever is easiest for you to
understand & extend.
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top