While loop help

  • Thread starter thomasancilleri
  • Start date
D

Dave Angel

sorry i just started using google groups, not really all that sure how to use it properly.

IMHO, best way is to switch to a good email program, and mail your
messages to comp.lang.python. That's after subscribing via
http://mail.python.org/mailman/listinfo/python-list

You can set up filters in your email program so all forum messages to
into a separate folder, one per forum. And you tell your email program
to use text mail, not html.
 
J

jmfauth

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 tothe 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:
.... 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
 
D

Dave Angel

Try putting these lines into a Python script:

import sys
print(sys.version)
That works (of course), but in every Python version I've seen, one merely
needs to invoke the python interactive interpreter and the banner is
displayed:

$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.$

And if several are installed, that isn't necessarily the one that'll run
when one runs a script. Depends on how the script is invoked (and on
what OS is running), and on the shebang line, PATH, etc.

The real point about those two lines is that they can be added to most
scripts.
 
C

Chris Angelico

On Wed, 10 Apr 2013 02:10:29 +1000, Chris Angelico wrote:

... I'm not sure what version I'm using ...

Try putting these lines into a Python script:

import sys print(sys.version)

That works (of course), but in every Python version I've seen, one
merely needs to invoke the python interactive interpreter and the
banner is displayed:

$ python Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2 Type "help",
"copyright", "credits" or "license" for more information.
quit()
$
And if several are installed, that isn't necessarily the one that'll run
when one runs a script. Depends on how the script is invoked (and on
what OS is running), and on the shebang line, PATH, etc.

The real point about those two lines is that they can be added to most
scripts.

Well yes, but if multiple versions are installed and the script has a
shebang, then invoking the same interpreter as the shebang does will
produce the same result.

I still went with a guaranteed-safe option. Adding those two lines to
his script is sure to report on the Python being used to run the
script, and it's not as if it's a massively-complex incantation :)
But this is dancing on the head of a pin anyway; OP just didn't know what
version of Python he was running, so he is extremely unlikely to have
more than one version installed, and to be choosing amongst them.

Dunno about that. It's pretty easy to have two versions of something
without understanding why.

ChrisA
 
R

rusi

... 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!

Not perhaps a direct answer...
Anyways there is style in which python is best used which people
coming from more traditional languages are usually not familiar with:
its called 'playing around in the interpreter'

Here is a small session based on your code that shows this interaction
with the interpreter:
---------------------
$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in <module>

---------------------

Notice some things here:
1. I check out the code as soon as its written. So when I cutpasted
from your code, without keeping names (m <-> number) consistent, I get
an error, correct it and continue
2. There is not a single print statement. Not just the functions have
no prints, even the code that calls them has none. Just call get
answer.
This point needs to be underscored: In C or java you cannot write any
useful code without doing IO ie printf/scanf etc. In python you can
and you should try to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top