Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43171

Re: While loop help

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 <rosuav@gmail.com>
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 <b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com>
References <b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com>
Date Wed, 10 Apr 2013 00:00:08 +1000
Subject Re: While loop help
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.346.1365516017.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Tue, Apr 9, 2013 at 11:32 PM,  <thomasancilleri@gmail.com> 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 loops the first 2 lines. Can someone provide a detailed beginner friendly explanation. 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 = "true"
> while restart == "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 = 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 == 1:
>         number = 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 = "Inches to Meters"
>         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:
>         number = 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 = "Milliliters to Pints"
>         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:
>         number = int(raw_input("\n\nType the amount in Kilometers you would like to convert to Miles.\n"))
>         operation = "Kilometers to Miles"
>         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")

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

While loop help thomasancilleri@gmail.com - 2013-04-09 06:32 -0700
  Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:57 -0700
    Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:12 -0400
      Re: While loop help thomasancilleri@gmail.com - 2013-04-09 10:18 -0700
        Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:23 +1000
        Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:30 -0400
  Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:58 -0700
    Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:05 +1000
      Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
        Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:02 -0400
        Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:10 +1000
          Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
            Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:36 -0400
            Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:47 +1000
              Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
                Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:08 +1000
                Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:27 -0400
              Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
          Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
          Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 19:35 +0000
            Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 16:12 -0400
              Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 20:59 +0000
                Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 08:36 +1000
        Re: While loop help rusi <rustompmody@gmail.com> - 2013-04-10 08:59 -0700
      Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
  Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:00 +1000
  Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:49 -0700
    Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-09 23:44 -0700
      Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-10 20:00 -0700
  Re: While loop help jmfauth <wxjmfauth@gmail.com> - 2013-04-09 12:19 -0700

csiph-web