【发布时间】:2013-05-06 23:18:43
【问题描述】:
我的功能是:
def searchstock():
dictionary=calcreturn(dictionize(tickers(openfile()),pairslist()))
inp=raw_input("What is the stock ticker? ")
while True:
try:
dictionary[inp]
break
except KeyError:
print("Ticker not found. Please input again ")
print(inp, dictionary[inp])
try/except 不起作用。我正在尝试查看用户输入是否在字典键中,然后返回键和相应的值
如果imp不在字典中,为什么会是一个无限循环?
【问题讨论】:
-
你能修复缩进吗?
-
它需要使用try/exept,而不是if/else
-
你不应该修改你原来的问题......因为现在看起来每个答案都只是复制并粘贴了你所拥有的(事实并非如此)
-
你想用循环来完成什么?
标签: python dictionary try-catch except