需要添加错误消息

时间:2016-04-09 21:05:15

标签: python python-2.7 for-loop dictionary

我一直在编写具有字典的代码并为用户提供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次任何想法

1 个答案:

答案 0 :(得分: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或只是查找" python以获取其他内容"。

在任何情况下,循环都是错误的方法。你有一本字典,所以请使用它!

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"