Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46747
| References | <55c2eb06-7b9f-480f-8668-5e19e236799f@googlegroups.com> |
|---|---|
| Date | 2013-06-02 18:44 -0700 |
| Subject | Re: Issue converting to string to integer. |
| From | Dan Stromberg <drsalists@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2570.1370223845.3114.python-list@python.org> (permalink) |
[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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web