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


Groups > comp.lang.python > #43171

Re: While loop help

References <b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com>
Date 2013-04-10 00:00 +1000
Subject Re: While loop help
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.346.1365516017.3114.python-list@python.org> (permalink)

Show all headers | 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