While loop help

2013-04-09 Thread thomasancilleri
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")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
Also I'm getting a invalid syntax next to my elif statements that i wasnt 
getting before. why is this happening now?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
On Tuesday, April 9, 2013 9:32:18 AM UTC-4, [email protected] 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. Also suddenly I'm getting an invalid syntax error next to 
> my elif statements when I wasn't a minute ago. What is wrong here?
> 
> 
> 
> #!/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")

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
For system version I get this:
2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

Also, I understand what your saying about the continuation code. There's no 
need for me to include it in each if/else statement, I could just use it at the 
end of the program outside of the statements and it would run no matter what. 
But, what I don't understand exactly is the while statement. I've been looking 
around a lot lately and notice that people say to use for instance:

while restart: or while true: or while restart = true:

What makes a statement true? Is there a way to return a true or false message. 
My method was to ask the user to type "true" and if that print statement 
matched restart = "true" then the loop would continue but i imagine there is a 
better way then matching strings and integers like i have been.

Also what confuses me is that python doesn't use brackets. How do I contain all 
of my if/else statements into one while loop? Do I have to indent each line of 
code and extra indentation? I'm used to highschool doing c++ and java when I 
would just say:

while (x<3) 
{
 if ()
else ()
}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
So what would be the proper way to perform a loop of this program. I still 
can't quite figure out the best way to do it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
I responded before I saw this message, this was very helpful so I appreciate 
your quick and helpful responses. So do you think prompting for a string and 
then checking if the string is true is a good practice for something like this? 
When would checking for true/false be necessary?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: While loop help

2013-04-09 Thread thomasancilleri
sorry i just started using google groups, not really all that sure how to use 
it properly.
-- 
http://mail.python.org/mailman/listinfo/python-list