我在尝试找出在哪里要求用户在我的代码中输入公司名称时遇到问题,这是我到目前为止所做的:
def ticker(filename):
key = [] # company name
value = [] # ticker symbol
with open(filename) as f:
lst = [line.rstrip() for line in f]
for line in range(0, len(lst), 2):
key.append(lst[line])
for line in range(1, len(lst), 2):
value.append(lst[line])
dictionary = dict(zip(key, value))
我试图要求用户输入公司名称,输入时代码返回公司的股票代码。上面的代码工作,我只是不知道在哪里要求用户输入以及如何获取代码返回符号。
希望我的问题有道理......谢谢你的帮助!
答案 0 :(得分:0)
添加这一小段代码,应解决您的问题:
user_input = raw_input("Please enter a company name: ")
if user_input in dictionary:
print "Ticker symbol for ", user_input, " is: ", dictionary[user_input]
else:
print user_input, " not a valid company name"
用户输入字典密钥(本例中为公司名称)
dictionary [user_input]为我们提供了该键的值。