Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46744 > unrolled thread
| Started by | Fdama <fsdama@gmail.com> |
|---|---|
| First post | 2013-06-02 18:12 -0700 |
| Last post | 2013-06-02 19:02 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Issue converting to string to integer. Fdama <fsdama@gmail.com> - 2013-06-02 18:12 -0700
Re: Issue converting to string to integer. Dan Stromberg <drsalists@gmail.com> - 2013-06-02 18:44 -0700
Re: Issue converting to string to integer. Dan Sommers <dan@tombstonezero.net> - 2013-06-03 01:52 +0000
Re: Issue converting to string to integer. Chris Angelico <rosuav@gmail.com> - 2013-06-03 12:08 +1000
Re: Issue converting to string to integer. alex23 <wuwei23@gmail.com> - 2013-06-02 19:02 -0700
| From | Fdama <fsdama@gmail.com> |
|---|---|
| Date | 2013-06-02 18:12 -0700 |
| Subject | Issue converting to string to integer. |
| Message-ID | <55c2eb06-7b9f-480f-8668-5e19e236799f@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2013-06-02 18:44 -0700 |
| Message-ID | <mailman.2570.1370223845.3114.python-list@python.org> |
| In reply to | #46744 |
[Multipart message — attachments visible in raw view] — view raw
On Sun, Jun 2, 2013 at 6:12 PM, Fdama <fsdama@gmail.com> wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Dan Sommers <dan@tombstonezero.net> |
|---|---|
| Date | 2013-06-03 01:52 +0000 |
| Message-ID | <DFSqt.45780$CG1.40950@newsfe21.iad> |
| In reply to | #46744 |
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.
Dan
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-06-03 12:08 +1000 |
| Message-ID | <mailman.2574.1370225290.3114.python-list@python.org> |
| In reply to | #46748 |
On Mon, Jun 3, 2013 at 11:52 AM, Dan Sommers <dan@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
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-06-02 19:02 -0700 |
| Message-ID | <de27ee4e-5e21-47ad-9551-49e8c89de38f@ua8g2000pbb.googlegroups.com> |
| In reply to | #46744 |
On Jun 3, 11:12 am, Fdama <fsd...@gmail.com> 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?
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web