While loop help

jmfauth wxjmfauth at gmail.com
Tue Apr 9 15:19:55 EDT 2013


On 9 avr, 15:32, thomasancill... at 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.
>
> #!/usr/bin/env python
> restart = "true"
> while restart == "true":
> #Program starts here
>     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")
>
> #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"))
>         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"))
>         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")

-----

More (very) important:

meter: lower case "m"
kilometre: lower case "k"
milli: lower case "m"

http://www.bipm.org/en/home/



Less important:

Start with something simple and increase the complexity eg:

>>> # Py 3.2
>>> while True:
...     s = input('km: ')
...     if s == 'q':
...         break
...     a = float(s)
...     print('{} [kilometre] == {} [metre]'.format(a, a * 1000))
...
km: 1
1.0 [kilometre] == 1000.0 [metre]
km: 1.3456
1.3456 [kilometre] == 1345.6 [metre]
km: q

jmf




More information about the Python-list mailing list