2017-10-15 18 views
0

ある測定値を別の測定値に変換するコードを作成しました。 (私は辞書を使用することはできません) NameError:name 'num2'は定義されていません。つまり、2番目のループのif文が決して真実にならないことを意味します。それでも、何がうまくいかなかったのか分かりません。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'です。一致が見つかったときに '中断したい 'とし、' else'ブロックをあなたのforループに追加して、ユニットが不明であると不平を言います... –

+0

私は最初の部分でリターンを使用したことがないことを知りました。みんな、ありがとう! – Lexi

答えて

0

これは(raw_inputを使用して)ユーザからの入力を取得する上でいくつかのマイナーな変更であなたのコードです。 (あなたはあなたの関数のスコープ内の変数を設定しようとしていたとして)。また、あなたの入力を返し、あなたのケースでwhich1、およびwhich2Noneた:

# 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) 

これはcmmm値を変換するためのスクリプトのサンプル出力です:

$ 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:inputeval(raw_input(prompt))に等しく、それは他のユースケースを持って、あなたが引用符であなたの入力文字列を具現化するために持っているとして、あなたはそれを行う必要はありません!単にraw_inputを使用してください。

+0

それは助け!ありがとう! :D – Lexi

関連する問題