While loop help

thomasancilleri at gmail.com thomasancilleri at gmail.com
Tue Apr 9 11:47:05 EDT 2013


Sorry I'm just starting to learn python and I'm not sure what version I'm using to be quite honest. I just started typing it up in Komodo edit (Which I'm not personally a fan in particular) I fixed the missing parenthesis which fixed the invalid syntax problem. Also, I apologize for submitting code that did not run. This is the code I have now before I tried looping it again:

#!/usr/bin/env python

#Program starts here
print "To start the Unit Converter please type the number next to the conversion you would like to perform"
choice = raw_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:
    #operation = "Inches to Meters"
    number = int(raw_input("\n\nType the amount in Inches you would like to convert to Meters.\n"))
    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:
    #operation = "Milliliters to Pints"    
    number = int(raw_input("\n\nType the amount in Milliliters you would like to convert to Pints.\n"))
    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:
    #operation = "Kilometers to Miles"
    number = int(raw_input("\n\nType the amount in Kilometers you would like to convert to Miles.\n"))
    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")

Not sure what you meant to exactly by this:
"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"

Thanks for your reply and if you have any ideas for me to improve my coding that will prevent me from learning python in a sloppy way. I'd like to learn it correctly the first time!



More information about the Python-list mailing list