with open(filename, 'r') as source:
text = source.read()
place_to_names = dict([line.split(r',') for line in text.split()])
while True:
name = raw_input('Enter a name:')
print("%s lives in %s" % (name, places_to_names[name]))
text_file = open('pathtoFile', 'r').read()
text = text_file.split()
#turn the text into a dictionary
names_dic = []
for x in text:
x = x.split(',')
names_dic.append(x)
names_dic = dict(names_dic)
print names_dic #for testing
# asking a user to enter a name
name = "not_in_dic"
while name not in names_dic:
name = raw_input("Enter the name? ")
print name, "lives in ", names_dic[name]
阿内塔建议用一种更像Python的方式来做同样的事情
我会这样做:
相关问题 更多 >
编程相关推荐