A few years ago I found a conversion program on here for length and weight conversion. A friend of mine recently started a project where he needs this piece of code, but the project requires him to use the latest version of Python. I promised him I would help him as much as I could, but not having coded in python for years I can't really do much. I hope you guys can help. Whenever I run it I get the following two errors:
Traceback (most recent call last):
File "C:\Users\Tiberius\Documents\Konvertering.py", line 75, in <module>
main()
File "C:\Users\Tiberius\Documents\Konvertering.py", line 66, in main
s = raw_input("konverter hvad? (ex: 10 meter til fod) ")
NameError: global name 'raw_input' is not defined
The text is in danish btw. The code follows here:
units = {'kg': ('vægt', 1.),
'kilo': ('vægt', 1.),
'kilogram': ('vægt', 1.),
'lbs': ('vægt', 2.204),
'pund': ('vægt', 2.204),
'ton': ('vægt', 0.001),
'gram': ('vægt', 1000.),
'ounce': ('vægt', 35.27),
'm': ('afstand', 1.),
'meter': ('afstand', 1.),
'kilometer': ('afstand', 0.001),
'km': ('afstand', 0.001),
'centimeter': ('afstand', 100.),
'cm': ('afstand', 100.),
'meter': ('afstand', 1.),
'mil': ('afstand', 0.0006214),
'furlong': ('afstand', 0.004971),
'league': ('afstand', 0.0002071),
'fod': ('afstand', 3.281),
'fod': ('afstand', 3.281),
'tomme': ('afstand', 39.37)}
def getUnit(unit_name):
if unit_name in units:
return units[unit_name]
else:
raise ValueError("ikke genkendt enhed '{0}'".format(unit_name))
def convert(amt, from_unit, to_unit):
typeA, numA = getUnit(from_unit)
typeB, numB = getUnit(to_unit)
if typeA==typeB:
return amt * numB / numA
else:
raise ValueError("enheder er af forskellige kategori ('{0}' and '{1}')".format(typeA, typeB))
def conversion(s):
"""
Fortag enhedskonvertering
Der accepteres en string i forment
"(tal) (enhedA) [til] (enhedB)"
Hvis enhed A og enhed B er af den samme type, vend tilbage med svaret .
"""
s = s.strip().lower().split()
if len(s) not in (3, 4):
raise ValueError("Argument string har et forkert antal ord (skal være mellem tre eller fire)")
try:
amt = float(s[0])
except ValueError:
raise ValueError("argument string skal starte med et tal")
from_unit = s[1]
to_unit = s[-1]
return convert(amt, from_unit, to_unit)
def tryAgain():
s = raw_input('prøv igen? (Y/n)? ').strip().lower()
return 'yes'.startswith(s)
def main():
while True:
s = raw_input("konverter hvad? (ex: 10 meter til fod) ")
try:
print(": {0}".format(conversion(s)))
except ValueError as v:
print (v)
if not tryAgain():
break
if __name__=="__main__":
main()