我编写了一个程序,显示哪些国家在人类发展方面被分类为“非常高”、“高”、“中等”或“低”。国家将被分类如下:
- “非常高”,0.75 < HDI 得分
- “高”............,0.51 < HDI < 0.75
- “中”..., 0.26 < HDI < 0.5
- “低”............,......HDI < 0.26
结果应如下所示:
Country HDI score
Japan 0.903
Finland 0.895
Qatar 0.856
Lebanon 0.763
Armenia 0.743
Iraq 0.649
Pakistan 0.550
Djibouti 0.473
Cameroon 0.518
用我写的代码:
Country= ["Japan","Finland","Qatar","Lebanon","Armenia","Iraq","Pakistan",
"Djibouti","Cameroon"]
Country[0]=0.903
Country[1]=0.895
Country[2]=0.856
Country[3]=0.763
Country[4]=0.743
Country[5]=0.649
Country[6]=0.550
Country[7]=0.473
Country[8]=0.518
for Country in Country:
if Country>0.76:
print [Country, "Very High"]
elif 0.76>Country>0.51:
print [Country,"High"]
elif 0.51>Country>0.26:
print [Country,"Medium"]
elif Country < 0.26:
print [Country,"Low"]
但该代码的问题在于,它显示 HDI 和排名,而不是国家/地区名称和排名,如下所示:
[0.903, 'Very High']
[0.895, 'Very High']
[0.856, 'Very High']
[0.763, 'Very High']
[0.743, 'High']
[0.649, 'High']
[0.55, 'High']
[0.473, 'Medium']
[0.518, 'High']
我不明白我在这里缺少什么,为什么它打印 HDI 而不是相应的国家/地区名称,尽管我首先介绍了国家/地区名称。
最佳答案
这里似乎有一些问题......
您使用for Country in Country
..声明...(让我很困惑)。无论如何,使用国家作为列表名称:
Countries = ["Japan","Finland","Qatar","Lebanon","Armenia","Iraq","Pakistan",
"Djibouti","Cameroon"]
Countries[0]=0.903 # overriding in Countries the 'Japan' Country name with the value '0.903'
... snippet...
for Country in Countries:
...snippet...
你也几乎混合了列表和字典。让我们坚持使用列表+元组。
尝试:
Countries = [('Japan', 0.903), (country, value), etc...]
然后你会得到:
for country in Countries:
my_country_name = country[0]
my_country_value = country[1]
if my_country_value >0.76:
print [my_country_name, "Very High"]
elif ... :
print ...
etc..
..snippet...
享受吧!
关于python - 表格显示代码显示错误的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47823900/