无法从字典中显示用户输入键的值

时间:2013-11-25 18:41:25

标签: python

我有选项只需要知道如何从用户输入键获取值。我试过的所有方法都会返回一个关键错误。到目前为止,这是我的代码:

names= open('names.dat', 'r')
sonfather = names.read().split(',')

new_sonfather = []
for item in sonfather:
    new_sonfather.append(item.split(":"))
sonfather=new_sonfather

son_father_dictionary=dict(sonfather)
print son_father_dictionary


father_grandfather_dictionary=dict(son_father_dictionary)
print father_grandfather_dictionary

print son_father_dictionary.values()
print son_father_dictionary.keys()

print "0 - Quit"
print "1 - Find a Father"
print "2 - Find a Grandfather"
print "3 - Find a Son"
print "4 - Find a Grandson"
print "5 - List of names"

control = ""
while control != "quit":
    choice = input("Enter your choice here: ")
    if choice == 0:
        control = "quit"
        print("Thank you for using father/son finder")
    elif choice == 1:
        son = raw_input("Enter the name of the son: ")
        for son in son_father_dictionary:
            father=son_father_dictionary.get(son) 
            print father
            #I can return all the values in the dictionary, but I only need the 
             value of the key that is entered for son

1 个答案:

答案 0 :(得分:0)

control = True
while control:
    choice = input("Enter your choice here: ")
    control = int(sys.stdin.read(1))

    if choice == 1:
       son = raw_input("Enter the name of the son: ")