2017-10-15 71 views
0

我做了一个代码,将一个测量转换为另一个。 (我不允许使用字典) 我不停地收到一条错误消息:NameError:name'num2'没有被定义,这意味着第二个循环中的if语句永远不会变成true我猜。不过,我不知道哪里出了问题。Python,公制转换程序,无字典

任何想法将不胜感激。谢谢!

# Metric conversion program 

def displayWelcome(): 
    print('This program converts convert one length unit into another unit') 
    print('Following are the available units: (mm), (cm), (m), (km), (inches), (ft), (yds), (miles)') 

def getConvertfrom(): 
    which1 = input('Which unit would you like to convert from: ') 

def getConvertto():  
    which2 = input ('Which unit would you like to convert to: ') 


num1 = float(input('Enter a value: ')) 

available_units = ('mm', 'cm', 'm', 'km', 'inches', 'ft', 'yds', 'miles') 
conversions = (1, 10, 1000, 1e6, 25.4, 304.8, 914.4, 1.609344e6) 

# Display program welcome 
displayWelcome() 

# Get which conversion 
which1 = getConvertfrom() 
which2 = getConvertto() 



index = 0 
for i in range (0, len(available_units)): 
    if available_units[i] == str(which1): 
     num_in_mm = num1 * conversions[i] 

for j in range (0, len(available_units)): 
    if available_units[j] == str(which2): 
     num2 = num_in_mm/conversions[j] 

print(num1, which1, "is equal to", num2, which2) 
+0

'range(0,len(available_units))' - >'range(len(available_units))'。 'which1'已经是一个字符串了,所以'str(which1)' - >'which1'。当找到一个匹配项时,你会想'break',然后在你的for循环中添加一个'else'块,它会抱怨这个单元是未知的...... –

+0

我发现我从来没有在第一部分使用过return。感谢大家! – Lexi

回答

0

这是对(使用raw_input)从用户获取输入一些小的改动你的代码。此外返回你的输入,你的情况which1which2None(如你试图设置你的函数范围内的变量):

# Metric conversion program 

def displayWelcome(): 
    print('This program converts convert one length unit into another unit') 
    print('Following are the available units: (mm), (cm), (m), (km), (inches), (ft), (yds), (miles)') 

def getConvertfrom(): 
    return raw_input('Which unit would you like to convert from: ') 

def getConvertto(): 
    return raw_input('Which unit would you like to convert to: ') 


num1 = float(raw_input('Enter a value: ')) 

available_units = ('mm', 'cm', 'm', 'km', 'inches', 'ft', 'yds', 'miles') 
conversions = (1, 10, 1000, 1e6, 25.4, 304.8, 914.4, 1.609344e6) 

# Display program welcome 
displayWelcome() 

# Get which conversion 
which1 = getConvertfrom() 
which2 = getConvertto() 

index = 0 
for i in range (0, len(available_units)): 
    print available_units[i], '==', str(which1) 
    if available_units[i] == str(which1): 
     num_in_mm = num1 * conversions[i] 

for j in range (0, len(available_units)): 
    if available_units[j] == str(which2): 
     num2 = num_in_mm/conversions[j] 

print(num1, which1, "is equal to", num2, which2) 

这是脚本的样本输出mm值转换为cm

$ python testtest.py 
Enter a value: 1000 
This program converts convert one length unit into another unit 
Following are the available units: (mm), (cm), (m), (km), (inches), (ft), (yds), (miles) 
Which unit would you like to convert from: mm 
Which unit would you like to convert to: cm 
given params: which1: mm, which2: cm 
mm == mm 
cm == mm 
m == mm 
km == mm 
inches == mm 
ft == mm 
yds == mm 
miles == mm 
(1000.0, 'mm', 'is equal to', 100.0, 'cm') 

NB:input等于eval(raw_input(prompt)),你不需要做,因为它有其他用途的情况下,你必须体现在报价您输入的字符串!只需使用raw_input即可。

+0

它帮助!谢谢! :d – Lexi