2016-04-09 91 views
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倍任何想法,爲什麼

回答

0

因爲無論if語句將執行對於循環中的每一次迭代,即對於每個孩子一次。這就是for循環應該做的。

什麼你正在嘗試做的可能是這樣完成的:

found = False 
for i in child2parents: 
    if i == childs_name: 
     print " this childs mothers name is %s" % (child2parents[i]['mother']) 
     found = True 
if not found: 
    print "sorry no entry matches that name" 

這是否有意義?然而,這是一個共同的模式,並在Python有一些喜歡的選擇:

for i in child2parents: 
    if i == childs_name: 
     print " this childs mothers name is %s" % (child2parents[i]['mother']) 
     break 
else: 
    print "sorry no entry matches that name" 

如果您想了解更多有關此方法,你可以閱讀here或只是仰望「蟒蛇爲突破其他」。

在任何情況下,循環都是錯誤的方法。你有一本字典,請使用它!

if childs_name in child2parents: 
    print " this childs mothers name is %s" % (child2parents[childs_name]['mother']) 
else: 
    print "sorry no entry matches that name" 

其他方面的改進也可以做成:

child2parents = {'andrew': {'father': 'john', 'mother': 'jane'}, 'betsy': {'father': 'nigel', 'mother': 'ellen'}, 'louise': {'father': 'louis', 'mother': 'natalie'}, 'chad': {'father': 'joseph', 'mother': 'mary'}} 

# You've already taken care of ending the loop when the user enters 4 
# with the break statement below, no need to repeat it here. In any case 
# you were using 'x' but never assigning to it because you use 'choice' below. 
while True: 

    # Converting to an int is not a bad idea, but it will throw an error 
    # if the user enters a non-number and that's not a nice user experience. 
    # A simple solution is to stick to strings 
    choice = raw_input("1 = mother, 2 = father, 3 = both parents, 4 = stop ") 

    # This way you don't have to repeat the same logic 3 times. 
    if choice in ('1', '2', '3'): 

     # In some cases you had parentheses around the right hand side which 
     # weren't needed 
     childs_name = raw_input("please enter childs name here ") 

     if childs_name in child2parents: 
      parents = child2parents[childs_name] 
     else: 
      print "sorry no entry matches that name" 

      # Make sure that we go back to the start of the loop body 
      # instead of continuing down to the code below since 'parents' 
      # is not defined 
      continue 

    if choice == '1': 
     print "this childs mothers name is %s" % (parents['mother']) 
    elif choice == '2': 
     print "this childs fathers name is %s" % (parents['father']) 
    elif choice == '3': 
     print "this childs mothers name is %s and the childs fathers name is %s " % (parents['mother'], parents['father']) 

    elif choice == '4': 
     break 

    else: 
     print "sorry not a valid option" 
+0

非常感謝您爲您的所有輸入 – zero2008

相關問題