[Tutor] More Converter

Alan Gauld alan.gauld at btinternet.com
Sat Mar 22 00:24:56 CET 2008

<wackedd at mac.com> wrote

> Original = raw_input("Insert inches, feet ")
> To = raw_input("Insert inches, feet ")
> Variable = int(raw_input("Insert Amount to Convert "))

Up to here is OK.

> if Original == raw_input(feet) and To == raw_input(inches):

But here you get a bit confused.
raw_input is used to get input from the user as a string.
You have already stored the input so now you want to
compare that input to a literal value so you don't need
to use raw_input here, just compare to the actual string:

if Original == "feet" and To == "inches":
        print Variable*12

And that will work.
However to make your program easier for the
user - less typing and less chance of error you might
like to present a menu so that the user only has to
type one number or letter

Unit_Menu = """
1)    Yards
2)    Feet
3)    Inches

Select a unit(1-3)"""

>From = raw_input(Unit_Menu)
To = raw_input(Unit_Menu)

if From == "2" and To == "3":
  print From * 12

> I always get this error message though
> Traceback (most recent call last):
>  File "/Users/donaldlucas/Documents/Python Scripts/Converter.py", 
> line 5, in <module>
>    if Original == raw_input(feet) and To == raw_input(inches):
> NameError: name 'feet' is not defined

raw_input expects a string or a string variable. Without quotes
Python expects feet to be a variable name but no suh variable
has been defined. But as above you don;t need raw_input,
just compare to the literal string "feet".


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list