Hey guys,
I've been lurking around these forums for a few weeks now, and this website is by far the most helpful of any site I've visited as far as honing my newfound Python skills, and I figured it's time I jump in and ask some of the more specific questions that have been plaguing me. So here goes.
I'm working on my first non-school-assigned program, just a simple unit converter, and I'm having some trouble with the raw_input() method. If I code it like the following code block, everything works fine:
def distanceConverter(dfrom, funit, dto, tunit):
print dfrom
print dto
print funit
print tunit
def distance():
msg="1) Millimeters \n"
msg+="2) Centimeters \n"
print msg
#prints list of from options
choice=input("Select unit to convert from:")
if choice==1:
dfrom=1
funit="millimeters"
elif choice==2:
dfrom=2
funit="centimeters"
else:
print "Invalid entry. Please select 1 or 2"
print msg
choice=input("Select unit to convert to:")
if choice==1:
dto=1
tunit="millimeters"
elif choice==2:
dto=2
tunit="centimeters"
else:
print "Invalid entry. Please select 1 or 2"
distanceConverter(dfrom,funit,dto,tunit)
def mass():
print "mass function tester"
def velocity():
print "velocity function tester"
def main():
msg="Welcome to Rob's Unit Converter Build 1.0."
print msg
#prints the opening message and list of unit options
msg="1) Distance \n"
msg+="2) Mass/Weight \n"
msg+="3) Velocity"
print msg
choice=4
while choice>3:
choice=input("Please select the unit type:")
if choice==1:
distance()
elif choice==2:
mass()
elif choice==3:
velocity()
else:
print "Invalid selection, please enter 1, 2, or 3."
if __name__=='__main__':
main()
The problem is in the distance() function. If I use input() like I did above, and prompt for an integer value, everything goes smoothly. But, if I use raw_input(), and prompt for a string, like the following, (so the user can type 'millimeters' or 'centimeters' instead of just 1 or 2), I get this error: "UnboundLocalError: local variable 'dfrom' referenced before assignment."
def distanceConverter(dfrom, funit, dto, tunit):
print dfrom
print dto
print funit
print tunit
def distance():
msg="1) Millimeters \n"
msg+="2) Centimeters \n"
print msg
#prints list of from options
choice=raw_input("Select unit to convert from:")
if choice==('1','millimeters','Millimeters'):
dfrom=1
funit="millimeters"
elif choice==('2','centimeters','Centimeters'):
dfrom=2
funit="centimeters"
else:
print "Invalid entry. Please select 1 or 2"
print msg
choice=raw_input("Select unit to convert to:")
if choice==('1','millimeters','Millimeters'):
dto=1
tunit="millimeters"
elif choice==('2','centimeters','Centimeters'):
dto=2
tunit="centimeters"
else:
print "Invalid entry. Please select 1 or 2"
distanceConverter(dfrom,funit,dto,tunit)
def mass():
print "mass function tester"
def velocity():
print "velocity function tester"
def main():
msg="Welcome to Rob's Unit Converter Build 1.0."
print msg
#prints the opening message and list of unit options
msg="1) Distance \n"
msg+="2) Mass/Weight \n"
msg+="3) Velocity"
print msg
choice=4
while choice>3:
choice=input("Please select the unit type:")
if choice==1:
distance()
elif choice==2:
mass()
elif choice==3:
velocity()
else:
print "Invalid selection, please enter 1, 2, or 3."
if __name__=='__main__':
main()
Does anyone have any ideas as to what's causing the error?
NOTE: I am very new to programming, and I'm sure that this program can be written MUCH more efficiently, but this is by far the most complex program I've written to date, so I'm just trying to get it working before I mess with making it more efficient or implementing a GUI. However, if anyone has any tips for making the program more efficient, I'm all ears. Or eyes. You get it.
Also, I realize that this is a lot of code, but I've slimmed it down a lot already lol.
Also note that you need to input 1 at the first prompt to access the distance() function in question, but I'm sure you already knew that.
This is being written for Python 2.7.1, if that matters at all.
Any and all help is much appreciated.