Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'exercise': 0.04; 'string': 0.09; '")': 0.09; 'get.': 0.09; 'literal': 0.09; 'subject:string': 0.09; 'valueerror:': 0.09; 'cc:addr:python- list': 0.11; '23,': 0.16; 'cc:name:python list': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'subject:Issue': 0.19; 'input': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'skip:p 30': 0.29; "doesn't": 0.30; 'converting': 0.30; 'skip:& 60': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'second,': 0.31; 'skip:! 10': 0.31; 'file': 0.32; '(most': 0.33; 'skip:s 30': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:" 50': 0.36; 'hi,': 0.36; 'two': 0.37; 'skip:& 10': 0.38; 'version,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'bad': 0.39; 'changed': 0.39; 'blank': 0.60; 'conversion': 0.61; "you're": 0.61; 'first': 0.61; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'invalid': 0.68; 'line,': 0.68; '10:': 0.84; 'non-empty.': 0.84; 'partial': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=XPuqChF+ydQbIzx7U9k25fH2KupC1ej0UtaWPpk2YZc=; b=irw9g1LF56EnouJx4S05qN7Uurw94p70artHEyVMbjRWHh1pvSaEcnTFxc2k1TqEn7 cXfuRTDVEoRenYZpUbnSYteKL1kKwiGAZQm0eJO32MVBYrjm/OKWNNEJPTfZdj8RZ12Q TooLdTwqY+rPV5M9JwncGyVdxdzneOGBWHfwLo6sX1BZKmMiZEuNi8Z5zc5hEzhb2qyz l8NE5EDhCE9hFOlBMB2WHS5nQp4UN64FHPMJuzNwAU0BCXzFcId+hP/r3xj/k7eWbOHD Uo026BK2m7XFL4i2HQYx9IKuv7qmtX7A5V48kCr3a53sGbTGWrk+hZZNDyQBAggulbY6 9PiA== MIME-Version: 1.0 X-Received: by 10.229.11.199 with SMTP id u7mr7028246qcu.42.1370223842451; Sun, 02 Jun 2013 18:44:02 -0700 (PDT) In-Reply-To: <55c2eb06-7b9f-480f-8668-5e19e236799f@googlegroups.com> References: <55c2eb06-7b9f-480f-8668-5e19e236799f@googlegroups.com> Date: Sun, 2 Jun 2013 18:44:02 -0700 Subject: Re: Issue converting to string to integer. From: Dan Stromberg To: Fdama Content-Type: multipart/alternative; boundary=001a11c3fecc6489d904de361941 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 112 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370223845 news.xs4all.nl 15930 [2001:888:2000:d::a6]:43503 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46747 --001a11c3fecc6489d904de361941 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jun 2, 2013 at 6:12 PM, Fdama 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 > > 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. --001a11c3fecc6489d904de361941 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

= On Sun, Jun 2, 2013 at 6:12 PM, Fdama <fsdama@gmail.com> wrot= e:
Hi,

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

start=3DNone =A0 =A0 =A0#initialise
while start !=3D"":
=A0 =A0 start=3Dinput("\nStart: ")

=A0 =A0 if start:
=A0 =A0 =A0 =A0 start=3Dint(start)
=A0 =A0 =A0 =A0 finish=3Dint(input("Finish: "))

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

I then changed the code to this:

start=3DNone =A0 =A0 =A0#initialise
while start !=3D"":
=A0 =A0 start=3Dint(input("\nStart: "))

=A0 =A0 if start:

=A0 =A0 =A0 =A0 finish=3Dint(input("Finish: "))

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

I combined the int conversion and the input on the same line, rather than t= o have two different statements. But =A0got an error message:

Traceback (most recent call last):
=A0 File "C:\Users\Faisal\Documents\python\pizza_slicer.py", line= 23, in <module>
=A0 =A0 start=3Dint(input("\nStart: "))
ValueError: invalid literal for int() with base 10: ''
Converting an empty string to base 10 doesn't fly, so if yo= u hit a blank line on the input(), you get a bad conversion.=A0 In the firs= t version, you're only converting to base 10 if the string is non-empty= .=A0 In the second, you're attempting to convert irrespective.

BTW, I'm partial to:

if not start:
=
=A0=A0 continue

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

--001a11c3fecc6489d904de361941--