簡體   English   中英

Python,公制轉換程序,沒有字典

[英]Python, metric conversion program, no dictionary

我制作了一個代碼,可以將一個測量值轉換為另一個測量值。 (我不允許使用字典)我不斷收到一條錯誤消息:NameError: name 'num2' is not defined,這意味着第二個循環中的 if 語句從未成為真我猜。 不過,我不知道出了什么問題。

任何想法將不勝感激。 謝謝!

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

這是您的代碼,在從用戶獲取輸入(使用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')

注意: input等於eval(raw_input(prompt)) ,您不需要這樣做,因為它有其他用例,您必須將輸入字符串包含在引號中! 只需使用raw_input。

轉換 = (1, 10, 1000, 1e6, 25.4, 304.8, 914.4, 1.609344e6) 關於這個? 我怎么能做到 metrix = {1 : [1000, 'Kilomer'], 2: [[1],'Megameter'], 3: 'Gigameter', 4: 'Terameter', 5: 'Petameter', 6 :'毫米',7:'微米',8:'納米',9:'皮米',10:'飛米'}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM