On Tue, Apr 9, 2013 at 11:32 PM, <thomasancill...@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 -- http://mail.python.org/mailman/listinfo/python-list