[英]Python, metric conversion program, no dictionary
I made a code which converts one measurement to another.我制作了一个代码,可以将一个测量值转换为另一个测量值。 (I'm not allowed to use dictionary) I keep getting an error message: NameError: name 'num2' is not defined, which means the if statement in the second loop never became true I guess.
(我不允许使用字典)我不断收到一条错误消息:NameError: name 'num2' is not defined,这意味着第二个循环中的 if 语句从未成为真我猜。 Still, I have no idea what went wrong.
不过,我不知道出了什么问题。
Any ideas will be appreciated.任何想法将不胜感激。 Thanks!
谢谢!
# 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)
This is your code with some minor changes on getting input from user (using raw_input
). 这是您的代码,在从用户获取输入(使用
raw_input
)时进行了一些小的更改。 Moreover return your input, in your case which1
, and which2
was None
(as you were trying to set variable inside of your function scope): 此外返回你的输入,你的情况
which1
和which2
是None
(如你试图设置你的函数范围内的变量):
# 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)
This is a sample output of the script for converting mm
value to cm
: 这是用于将
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
is equal to eval(raw_input(prompt))
and you don't need to do that as it has other use cases and you have to embody your input string in quotes! 注意:
input
等于eval(raw_input(prompt))
,您不需要这样做,因为它有其他用例,您必须将输入字符串包含在引号中! Simply use raw_input. 只需使用raw_input。
conversions = (1, 10, 1000, 1e6, 25.4, 304.8, 914.4, 1.609344e6) regarding about this ?转换 = (1, 10, 1000, 1e6, 25.4, 304.8, 914.4, 1.609344e6) 关于这个? How I could able to do metrix = {1 : [1000, 'Kilomer'], 2: [[1],'Megameter'], 3: 'Gigameter', 4: 'Terameter', 5: 'Petameter', 6: 'Millimeter', 7: 'Micrometer', 8: 'Nanometer',9: 'Picometer', 10: 'Femtometer'}
我怎么能做到 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.