所以,我有一个包含国家名称和相应股票指数的字典。要求用户进入五个不同的国家/地区,该程序将获取五个相应的股票指数并对数据执行某些操作。
当被要求输入时,如果可以找到国家,我会检查字典,这会发生五次。现在,处理异常的部分没有按照我的意愿行事。我的循环和/或异常处理有什么问题??
ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose
countries = []
print('Please choose five stock exchanges to analyse,' )
print('just name the corresponding countries \n')
for i in range(0, 5):
while True:
try:
countr = input('Please enter country no. %d: ' %(i+1))
countr = countr.title()
if countr in ex_dict.keys():
print('Found the corresponding stock index! \n')
countries.append(countr)
break
except KeyError:
print('Country not found, please try again! \n')
答案 0 :(得分:2)
此处不会有KeyError
因为您的代码永远不会厌烦访问字典,只需检查密钥是否在keys
。您可以简单地执行此操作以实现相同的逻辑:
ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose
countries = []
print('Please choose five stock exchanges to analyse,' )
print('just name the corresponding countries \n')
for i in range(0, 5):
while True:
countr = input('Please enter country no. %d: ' %(i+1))
countr = countr.title()
if countr in ex_dict.keys():
print('Found the corresponding stock index! \n')
countries.append(countr)
break
else:
print('Country not found, please try again! \n')
示例运行:
Please choose five stock exchanges to analyse,
just name the corresponding countries
Please enter country no. 1: sdf
Country not found, please try again!
Please enter country no. 1: USA
Found the corresponding stock index!
Please enter country no. 2: Aregtng
Country not found, please try again!
Please enter country no. 2: Argentina
Found the corresponding stock index!
Please enter country no. 3: United States
Found the corresponding stock index!
Please enter country no. 4: usa
Found the corresponding stock index!
Please enter country no. 5: usa
Found the corresponding stock index!
注意:.keys()
过度杀戮:要检查某个键是否在字典中,您只需要k in some_dict
答案 1 :(得分:0)
你的休息时间不在if范围内......所以它会在第一次尝试时中断。
答案 2 :(得分:0)
来自doc
只要请求dict()对象(使用格式a = adict [key])并且密钥不在字典中,Python就会引发KeyError。
在您的代码段中,如果您想在用户插入国家/地区字典中不存在的密钥时打印消息,则只需添加else语句而不是catch异常。
您可以通过以下方式更改代码:
if countr in ex_dict.keys():
print('Found the corresponding stock index! \n')
countries.append(countr)
break
else:
print('Country not found, please try again! \n')
答案 3 :(得分:0)
几点:
if key in dict.keys()
此外,还有很多循环。我认为for i in range(0,5)
已经足够了。不那么复杂。
ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose
countries = []
print('Please choose five stock exchanges to analyse,' )
print('just name the corresponding countries \n')
for i in range(0, 5):
countr = raw_input('Please enter country no. %d: ' %(i+1))
countr = countr.title()
if countr in ex_dict.keys():
print('Found the corresponding stock index! \n')
countries.append(countr)
else:
print('Country not found, please try again! \n')
<强>输出:强>
C:\Users\dinesh_pundkar\Desktop>python c.py
Please choose five stock exchanges to analyse,
just name the corresponding countries
Please enter country no. 1: USA
Found the corresponding stock index!
Please enter country no. 2: asd
Country not found, please try again!
Please enter country no. 3: asd
Country not found, please try again!
Please enter country no. 4: USA
Found the corresponding stock index!
Please enter country no. 5: United states
Found the corresponding stock index!
C:\Users\dinesh_pundkar\Desktop>
答案 4 :(得分:0)
我假设你想要它给你一条消息,如果股票不在字典ex_dict中,我已经修改了代码来做同样的事情。
ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose
countries = []
print('Please choose five stock exchanges to analyse,' )
print('just name the corresponding countries \n')
for i in range(0, 5):
while True:
try:
countr = input('Please enter country no. %d: ' %(i+1))
countr = countr.title()
if countr not in ex_dict.keys():
print ('Try again!')
else:
print('Found the corresponding stock index! \n')
countries.append(countr)