我的功能是:发现关键除了
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 /除了不工作。我试图查看用户输入是否在字典键中,然后返回键和相应的值
如果imp不在字典中,为什么这是一个无限循环?
你能修理缩进吗? – 2013-05-06 23:19:53
http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary – 2013-05-06 23:21:17
它需要使用try/exept,而不是如果/ else – user2356391 2013-05-06 23:25:15