Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'read.': 0.03; 'anyway.': 0.05; 'beginner': 0.05; 'elif': 0.05; 'insert': 0.05; '#if': 0.07; 'compiler': 0.07; 'continuation': 0.07; 'subject:help': 0.08; 'appropriate.': 0.09; 'converts': 0.09; 'lines.': 0.09; 'restart': 0.09; 'runs': 0.10; 'python': 0.11; '"to': 0.16; '"true":': 0.16; "(it's": 0.16; '(modulo': 0.16; '11:32': 0.16; 'correctness': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'looping': 0.16; 'loops': 0.16; 'measurement': 0.16; 'notably': 0.16; 'shorten': 0.16; 'there!': 0.16; 'skip:= 10': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; 'trying': 0.19; 'normally': 0.19; 'starts': 0.20; 'seems': 0.21; 'appears': 0.22; 'input': 0.22; 'putting': 0.22; 'print': 0.22; 'creating': 0.23; 'issue,': 0.24; 'please?': 0.24; 'skip:" 20': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'fixed': 0.29; "doesn't": 0.30; 'characters': 0.30; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'comments': 0.31; 'easier': 0.31; 'lines': 0.31; 'posting': 0.31; 'usually': 0.31; 'bad.': 0.31; 'correctly.': 0.31; 'long.': 0.31; 'figure': 0.32; 'run': 0.32; 'another': 0.32; 'quite': 0.32; 'skip:# 10': 0.33; 'style': 0.33; 'actual': 0.34; 'maybe': 0.34; 'problem': 0.35; 'basic': 0.35; 'received:209.85': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'in.': 0.36; 'next': 0.36; 'unit': 0.37; 'two': 0.37; 'level': 0.37; 'received:209': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'eventually': 0.60; 'units': 0.60; 'most': 0.60; 'tell': 0.60; 'conversion': 0.61; 'new': 0.61; 'entire': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'our': 0.64; 'provide': 0.64; 'more': 0.64; 'details': 0.65; 'here': 0.66; 'close': 0.67; 'line,': 0.68; 'surrounding': 0.68; 'email,': 0.69; 'friendly': 0.72; 'condition.': 0.84; 'copy-paste': 0.84; 'easier!': 0.84; 'meters': 0.84; 'safer': 0.84; 'working,': 0.84; 'inches': 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:content-type:content-transfer-encoding; bh=6Y8ZJ/+goRouIEwWnUnngbIRLmOybceZNuWemJoYr4U=; b=yvAnGNUE1zg9/Ip/45zV0eDXMgKTfIsUDP+Mem3JH18eCAeioyjW6dKMjhEbDh5r17 ZC4WoCUrJIDoSKQUAnsujlf5IrjXVRiRhHEXGEh91Y3E7XNiuc7c+n6ymWcag0Jz8dU4 yofoRl2Lyln8xxnV5mEllzIF7aDyWQ78CnFqTqb6A7QMXtkAx1uFXeK4g2U9UhGB490H q81Fgx+BG4RYKfbEWERhn4RBtcSmtKOAfLuwuNaWSCVlOMtbYO/ZHhIHMFN33hfyKfhe KPXKaoIbVFgwbEV8++4bhP3Yvc1kSTn5vqyVBBP9asKUDvMsQ1SoizxiYmlkyUfYpU/e Veow== MIME-Version: 1.0 X-Received: by 10.58.34.12 with SMTP id v12mr19045589vei.55.1365516008495; Tue, 09 Apr 2013 07:00:08 -0700 (PDT) In-Reply-To: References: Date: Wed, 10 Apr 2013 00:00:08 +1000 Subject: Re: While loop help From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365516017 news.xs4all.nl 6962 [2001:888:2000:d::a6]:60098 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43171 On Tue, Apr 9, 2013 at 11:32 PM, wrote: > I'm new to learning python and creating a basic program to convert units = of measurement which I will eventually expand upon but im trying to figure = out how to loop the entire program. When I insert a while loop it only loop= s the first 2 lines. Can someone provide a detailed beginner friendly expla= nation. Here is my program. Hi there! I'm going to make a few general comments about your code; this won't necessarily tell you why it's looping the "first two lines" (not sure quite what you mean there), but may be of use anyway. Firstly, your code actually doesn't run as-is. It doesn't loop *at all*. Please paste actual runnable code; it makes our job a lot easier! You have a copy-paste problem with one of your input lines (a missing close parenthesis). Once I fixed that, the program appears to loop quite correctly. > #!/usr/bin/env python > restart =3D "true" > while restart =3D=3D "true": > #Program starts here Putting your comments flush-left as though they were preprocessor directives to an old C compiler is unnecessary; indenting them to the same level as the surrounding code usually makes your code easier to read. > print "To start the Unit Converter please type the number next to the= conversion you would like to perform" > choice =3D input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres= to Square-Miles\n") This is BAD. VERY BAD. As evidenced by the print line, you are using Python 2 (btw, please specify; I tested your code in 2.7, but maybe your version is a bit different); the input() function in Python 2 will eval whatever the user types in. > #If user enters 1:Program converts inches to meters > if choice =3D=3D 1: > number =3D int(raw_input("\n\nType the amount in Inches you would= like to convert to Meters.\n")) This is a MUCH safer way to accept input. Use raw_input() and then convert it in whatever way is appropriate. > operation =3D "Inches to Meters" > calc =3D round(number * .0254, 2) > print "\n",number,"Inches =3D",calc,"Meters" > restart =3D raw_input("If you would like to perform another conve= rsion type: true\n" > > #If user enters 2:Program converts millimeters to pints > elif choice =3D=3D 2: > number =3D int(raw_input("\n\nType the amount in Milliliters you = would like to convert to Pints.\n")) Quite a few of your lines are getting long. That's not a particularly big problem normally (it's a style issue, not a code correctness one), but when you're posting in an email, it's usually safer to shorten the lines to 70-80 characters max; but make sure your code still runs correctly. > operation =3D "Milliliters to Pints" > calc =3D round(number * 0.0021134,2) > print "\n",number,"Milliliters =3D",calc,"Pints" > restart =3D raw_input("If you would like to perform another conve= rsion type: true\n") > > #If user enter 3:Program converts kilometers to miles > elif choice =3D=3D 3: > number =3D int(raw_input("\n\nType the amount in Kilometers you w= ould like to convert to Miles.\n")) > operation =3D "Kilometers to Miles" > calc =3D round(number * 0.62137,2) > print "\n",number,"Kilometers =3D",calc,"Miles" > restart =3D raw_input("If you would like to perform another conve= rsion type: true\n") 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. But, as I said, your code seems to work for me (modulo the missing parenthesis). Can you give more details about what's not working, please? ChrisA