尝试 return 嵌套字典中的键和值时出现问题
Issue trying to return key and values inside a nested dictionary
第一次张贴在这里。我正尝试在 Python 上为我正在从事的项目创建一个简单的转换程序,作为我注册的课程的一部分。
我正在尝试 return 基于起始单位的多个长度单位的转换。为此,我正在尝试使用嵌套词典。它必须通过 CLI 终端,所以我不允许使用 ttinker 或类似的东西来提供 GUI。
我正在尝试 return 格式:
x 厘米换算后等于:
x 公里
x 米
x 英里等
相反,我用我当前版本的代码得到这个 return(虽然我确实希望它像这样 return,但这是最接近我想要的结果的了):
x 厘米换算后等于:
x
x
x
公里
米
英里
我目前的代码如下:
len_selection = pyip.inputMenu(["Nanometre", "Micrometre", "Millimetre", "Centimetre", "Metre", "Kilometre", "Inch", "Foot", "Yard", "Mile", "Nautical Mile"], numbered=True)
print("\nEnter the number of your required unit\n")
len_value = pyip.inputNum("\nPlease enter the length to convert:")
length_conversion_factors = {
'kilometre': {'metre': 0.001, 'centimetre': 0.00001, 'millimetre': 0.000001,
'micrometre': 0.000000001, 'nanometre': 0.000000000001, 'mile': 1.60934,
'yard': 0.000914397727272727, 'foot': 0.000304799242424242, 'inch': 2.53999368686869,
'nautical mile': 1.852},
'metre': {'kilometre': 1000, 'centimetre': 0.01, 'millimetre': 0.001, 'micrometre': 0.000001,
'nanometre': 0.000000001, 'mile': 1609.34, 'yard': 0.914397727272727, 'foot': 0.304799242424242,
'inch': 0.0253999368686869, 'nautical mile': 1852},
'centimetre': {'kilometre': 100000, 'metre': 100, 'millimetre': 0.1,
'micrometre': 0.0001, 'nanometre': 0.0000001, 'mile': 160934,
'yard': 91.4397727272727, 'foot': 30.4799242424242, 'inch': 2.53999368686869,
'nautical mile': 185200}}
length_key = len_selection.lower()
print(f"\n{len_value} {len_selection}s converted equals:")
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value)
for key in length_conversion_factors[length_key]:
print(key)
我已经在这上面花了一天半的时间,现在我正在拔头发。
我已经尝试了'for key, value in length_conversion_factors[length_key]:'之类的方法,所以它都在一个for循环中运行,我得到一个值错误,说太多了被拆包。我也尝试过先解压密钥,将它们存储在一个变量中,然后使用打印语句,但也没有任何乐趣。
我试过使用 'return key' 和 'return conv_value',然后对它们使用单独的打印语句,但似乎根本无法让它工作。
我知道我可能漏掉了一些非常简单的东西。非常感谢
Python 让你在一个语句中打印多个东西:
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value, value)
这给我的输出如下:
1 kilometres converted equals:
0.001 metre
1e-05 centimetre
1e-06 millimetre
1e-09 micrometre
这似乎是您想要的。您甚至可以用 f 字符串替换最终的 print 语句,就像代码中已经出现的那样:print(f"{conv_value} {value}s")
第一次张贴在这里。我正尝试在 Python 上为我正在从事的项目创建一个简单的转换程序,作为我注册的课程的一部分。
我正在尝试 return 基于起始单位的多个长度单位的转换。为此,我正在尝试使用嵌套词典。它必须通过 CLI 终端,所以我不允许使用 ttinker 或类似的东西来提供 GUI。
我正在尝试 return 格式:
x 厘米换算后等于:
x 公里
x 米
x 英里等
相反,我用我当前版本的代码得到这个 return(虽然我确实希望它像这样 return,但这是最接近我想要的结果的了):
x 厘米换算后等于:
x
x
x
公里
米
英里
我目前的代码如下:
len_selection = pyip.inputMenu(["Nanometre", "Micrometre", "Millimetre", "Centimetre", "Metre", "Kilometre", "Inch", "Foot", "Yard", "Mile", "Nautical Mile"], numbered=True)
print("\nEnter the number of your required unit\n")
len_value = pyip.inputNum("\nPlease enter the length to convert:")
length_conversion_factors = {
'kilometre': {'metre': 0.001, 'centimetre': 0.00001, 'millimetre': 0.000001,
'micrometre': 0.000000001, 'nanometre': 0.000000000001, 'mile': 1.60934,
'yard': 0.000914397727272727, 'foot': 0.000304799242424242, 'inch': 2.53999368686869,
'nautical mile': 1.852},
'metre': {'kilometre': 1000, 'centimetre': 0.01, 'millimetre': 0.001, 'micrometre': 0.000001,
'nanometre': 0.000000001, 'mile': 1609.34, 'yard': 0.914397727272727, 'foot': 0.304799242424242,
'inch': 0.0253999368686869, 'nautical mile': 1852},
'centimetre': {'kilometre': 100000, 'metre': 100, 'millimetre': 0.1,
'micrometre': 0.0001, 'nanometre': 0.0000001, 'mile': 160934,
'yard': 91.4397727272727, 'foot': 30.4799242424242, 'inch': 2.53999368686869,
'nautical mile': 185200}}
length_key = len_selection.lower()
print(f"\n{len_value} {len_selection}s converted equals:")
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value)
for key in length_conversion_factors[length_key]:
print(key)
我已经在这上面花了一天半的时间,现在我正在拔头发。
我已经尝试了'for key, value in length_conversion_factors[length_key]:'之类的方法,所以它都在一个for循环中运行,我得到一个值错误,说太多了被拆包。我也尝试过先解压密钥,将它们存储在一个变量中,然后使用打印语句,但也没有任何乐趣。
我试过使用 'return key' 和 'return conv_value',然后对它们使用单独的打印语句,但似乎根本无法让它工作。
我知道我可能漏掉了一些非常简单的东西。非常感谢
Python 让你在一个语句中打印多个东西:
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value, value)
这给我的输出如下:
1 kilometres converted equals:
0.001 metre
1e-05 centimetre
1e-06 millimetre
1e-09 micrometre
这似乎是您想要的。您甚至可以用 f 字符串替换最终的 print 语句,就像代码中已经出现的那样:print(f"{conv_value} {value}s")