Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'elif': 0.05; 'string.': 0.05; '#if': 0.07; 'continuation': 0.07; 'subject:help': 0.08; 'string': 0.09; 'converted': 0.09; 'converts': 0.09; 'exception.': 0.09; 'restart': 0.09; 'statements': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"to': 0.16; 'integer.': 0.16; 'looping': 0.16; 'notably': 0.16; 'prevent': 0.16; 'skip:= 10': 0.16; 'wrote:': 0.18; 'typing': 0.19; 'meant': 0.20; 'starts': 0.20; 'coding': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'choices': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'fixed': 0.29; 'leave': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; '(which': 0.31; 'code': 0.31; 'submitting': 0.31; 'another': 0.32; 'quite': 0.32; 'url:python': 0.33; 'skip:# 10': 0.33; 'skip:& 30': 0.33; "i'd": 0.34; 'problem.': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'unit': 0.37; 'starting': 0.37; 'skip:& 10': 0.38; 'sure': 0.39; 'url:mail': 0.40; 'most': 0.60; 'conversion': 0.61; 'simply': 0.61; 'first': 0.61; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'reply': 0.66; 'invalid': 0.68; 'actually,': 0.84; 'condition.': 0.84; 'meters': 0.84; 'inches': 0.91; 'joel': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=n5V7RQgQ/HLcoeQAmk2096BRP/5qp1JTF9h6jA8Jafk=; b=DJ2OkVV0cLZ7Vx6KLV36eKI9AF9WekRDUOdRFTjZEbe/x0kKdn0L1/RrRjJ9Md5s/q Ez3zPDixv5iV67tfBE0eR/NeefdHNSvzXF+SZzn7NCgpxq4b1hDbZn2SC2cDYJw/Fn5q zIfVIt9a/9O9SIX3B0ruIl7Y0ezjLsuV55VPgEJPAMLbUeEa2osUUYKSpWkONN3rbfoW 5fwn0JNSJ6Qm3mSgc09MWbVEvAb0ZYSOmMzUIHwMyHQ3uD+ZaVnwk59f7IV/tiU0mgU1 SCpqOuHayQ/zArZdKHtl2HHfk/w9c/j1AovwgfmIOGZ7h+04kuR/9cfEyI6Pgt7PCZaf qxYQ== MIME-Version: 1.0 X-Received: by 10.58.12.69 with SMTP id w5mr19827370veb.16.1365523336031; Tue, 09 Apr 2013 09:02:16 -0700 (PDT) In-Reply-To: References: <596ca4b8-b5aa-4112-b086-6320108075f7@googlegroups.com> Date: Tue, 9 Apr 2013 12:02:15 -0400 Subject: Re: While loop help From: Joel Goldstick To: thomasancilleri@gmail.com Content-Type: multipart/alternative; boundary=047d7b2e4ab46076ae04d9efad49 Cc: "python-list@python.org" 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: 178 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365523346 news.xs4all.nl 2669 [2001:888:2000:d::a6]:36669 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43189 --047d7b2e4ab46076ae04d9efad49 Content-Type: text/plain; charset=UTF-8 On Tue, Apr 9, 2013 at 11:47 AM, wrote: > Sorry I'm just starting to learn python and I'm not sure what version I'm > using to be quite honest. I just started typing it up in Komodo edit (Which > I'm not personally a fan in particular) I fixed the missing parenthesis > which fixed the invalid syntax problem. Also, I apologize for submitting > code that did not run. This is the code I have now before I tried looping > it again: > > #!/usr/bin/env python > > #Program starts here > print "To start the Unit Converter please type the number next to the > conversion you would like to perform" > choice is not converted to an integer. raw_input returns a string. > choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to > Square-Miles\n") > This will convert choice to an int. Actually, it might not if what you type is not a number. Then it will cause and exception. But for now, do this: > choice = int(choice) > or leave choices as a string and make your if statements like if choice == '1' > #If user enters 1:Program converts inches to meters > if choice == 1: > #operation = "Inches to Meters" > number = int(raw_input("\n\nType the amount in Inches you would like > to convert to Meters.\n")) > calc = round(number * .0254, 2) > print "\n",number,"Inches =",calc,"Meters" > restart = raw_input("If you would like to perform another conversion > type: true\n") > > #If user enters 2:Program converts millimeters to pints > elif choice == 2: > #operation = "Milliliters to Pints" > number = int(raw_input("\n\nType the amount in Milliliters you would > like to convert to Pints.\n")) > calc = round(number * 0.0021134,2) > print "\n",number,"Milliliters =",calc,"Pints" > restart = raw_input("If you would like to perform another conversion > type: true\n") > > #If user enter 3:Program converts kilometers to miles > elif choice == 3: > #operation = "Kilometers to Miles" > number = int(raw_input("\n\nType the amount in Kilometers you would > like to convert to Miles.\n")) > calc = round(number * 0.62137,2) > print "\n",number,"Kilometers =",calc,"Miles" > restart = raw_input("If you would like to perform another conversion > type: true\n") > > Not sure what you meant to exactly by this: > "There's a lot of duplicated code here, most notably your continuation > condition. You can simply back-tab after the elif block and have some > code that reunites all the branches; this would also make things > clearer" > > Thanks for your reply and if you have any ideas for me to improve my > coding that will prevent me from learning python in a sloppy way. I'd like > to learn it correctly the first time! > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com --047d7b2e4ab46076ae04d9efad49 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Tue, Apr 9, 2013 at 11:47 AM, <thomasancilleri@gmail.c= om> wrote:
Sorry I'm just starting to learn python = and I'm not sure what version I'm using to be quite honest. I just = started typing it up in Komodo edit (Which I'm not personally a fan in = particular) I fixed the missing parenthesis which fixed the invalid syntax = problem. Also, I apologize for submitting code that did not run. This is th= e code I have now before I tried looping it again:

#!/usr/bin/env python

#Program starts here
print "To start the Unit Converter please type the number next to the = conversion you would like to perform"

<= /div>
choice is not converted to an integer.=C2=A0 raw_input returns a = string.
=C2=A0 =C2=A0
choice =3D raw_input("\n1:Inches to Meter\n2:Millileters to Pint= \n3:Acres to Square-Miles\n")

=C2=A0This will co= nvert choice to an int.=C2=A0 Actually, it might not if what you type is no= t a number.=C2=A0 Then it will cause and exception.
But for n= ow, do this:
choice =3D int(choic= e)

or leave choices as a string a= nd make your if statements like if choice =3D=3D '1'
=C2=A0
#If user enters 1:Program converts inches to meters
if choice =3D=3D 1:
=C2=A0 =C2=A0 #operation =3D "Inches to Meters"
=C2=A0 =C2=A0 number =3D int(raw_input("\n\nType the= amount in Inches you would like to convert to Meters.\n"))
=C2=A0 =C2=A0 calc =3D round(number * .0254, 2)
=C2=A0 =C2=A0 print "\n",number,"Inches =3D",calc,"= ;Meters"
=C2=A0 =C2=A0 restart =3D raw_input("If you would like to perfor= m another conversion type: true\n")

#If user enters 2:Program converts millimeters to pints
elif choice =3D=3D 2:
=C2=A0 =C2=A0 #operation =3D "Milliliters to Pints"
=C2=A0 =C2=A0 number =3D int(raw_input("\n\nType the= amount in Milliliters you would like to convert to Pints.\n"))
=C2=A0 =C2=A0 calc =3D round(number * 0.0021134,2)<= br> =C2=A0 =C2=A0 print "\n",number,"Milliliters =3D",calc,= "Pints"
=C2=A0 =C2=A0 restart =3D raw_input("If you would like to perform anot= her conversion type: true\n")

#If user enter 3:Program converts kilometers to miles
elif choice =3D=3D 3:
=C2=A0 =C2=A0 #operation =3D "Kilometers to Miles"
=C2=A0 =C2=A0 number =3D int(raw_input("\n\nType the= amount in Kilometers you would like to convert to Miles.\n"))
=C2=A0 =C2=A0 calc =3D round(number * 0.62137,2) =C2=A0 =C2=A0 print "\n",number,"Kilometers =3D",calc,&= quot;Miles"
=C2=A0 =C2=A0 restart =3D raw_input("If you would like to perform anot= her conversion type: true\n")

Not sure what you meant to exactly by this:
"There's a lot of duplicated code here, most not= ably your continuation
condition. You can simply back-tab after the elif block and have some
code that reunites all the branches; this would also make things
clearer"

Thanks for your reply and if you have any ideas for me to improve my = coding that will prevent me from learning python in a sloppy way. I'd l= ike to learn it correctly the first time!
--
http://mail.python.org/mailman/listinfo/python-list



--
--047d7b2e4ab46076ae04d9efad49--