0
我一直在寫有一本字典,併爲用戶提供了4個選項需要添加一個錯誤消息
1 = mothers name
2 = fathers name
3 = both names
4 = stop
如果名稱中的關鍵匹配用戶給它,它要求一個名稱的選項後,代碼它打印所需的選項的字典。 所有我想要做的代碼工作是添加一條錯誤消息,如果用戶輸入名稱不匹配字典中的任何內容。我的代碼如下
child2parents = {'andrew': {'father': 'john', 'mother': 'jane'}, 'betsy': {'father': 'nigel', 'mother': 'ellen'}, 'louise': {'father': 'louis', 'mother': 'natalie'}, 'chad': {'father': 'joseph', 'mother': 'mary'}}
x = ""
while x != 4:
choice = int(raw_input("1 = mother, 2 = father 3 = both parents 4 = stop "))
if choice == 1:
childs_name = raw_input("please enter childs name here ")
for i in child2parents:
if i == childs_name:
print " this childs mothers name is %s" % (child2parents[i]['mother'])
elif choice == 2:
childs_name = (raw_input("please enter childs name here "))
for i in child2parents:
if i == childs_name:
print " this childs fathers name is %s" % (child2parents[i]['father'])
elif choice == 3:
childs_name = (raw_input("please enter childs name here "))
for i in child2parents:
if i == childs_name:
print "this childs mothers name is %s and the childs fathers name is %s " % (child2parents[i]['mother'], child2parents[i]['father'])
elif choice == 4:
break
else:
print "sorry not a valid option"
,所以我嘗試添加一個錯誤消息像這樣
x = ""
while x != 4:
choice = int(raw_input("1 = mother, 2 = father 3 = both parents 4 = stop "))
if choice == 1:
childs_name = raw_input("please enter childs name here ")
for i in child2parents:
if i == childs_name:
print " this childs mothers name is %s" % (child2parents[i]['mother'])
if i != childs_name:
print "sorry no entry matches that name"
,但它顯示錯誤消息4倍任何想法,爲什麼
非常感謝您爲您的所有輸入 – zero2008