【问题标题】:Example program from Automate the Boring Stuff not working as described来自 Automate the Boring Stuff 的示例程序未按描述工作
【发布时间】:2019-11-18 01:39:45
【问题描述】:

我一直在浏览互联网,但在任何地方都没有看到这个,所以这一定是我做过的事情,但我不确定是什么。

Automate the Boring Stuff with Python 一书使用以下代码来解释字典:

    birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
    while True:
            print('Enter a name: (blank to quit)')
            name = input()
            if name == '':
                    break

    if name in birthdays:
            print(birthdays[name] + ' is the birthday of ' + name)
    else:
            print('I do not have birthday information for ' + name)
            print('What is their birthday?')
            bday = input()
            birthdays[name] = bday
            print('Birthday database updated.')

此代码不会为我生成任何错误,但是当我运行它时,当我尝试在字典中输入其中一个名称时它没有返回任何内容。如果我什么都不输入,程序会反应“我没有他们生日的生日信息?”

我尝试通过以下方式调整代码:

birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}

while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        break

    if name in birthdays:
        print(birthdays[name] + ' is the birthday of ' + name)
    else:
        print('I do not have birthday information for ' + name)
        print('What is their birthday?')
        bday = input()
        birthdays[name] = bday
        print('Birthday database updated.')

现在我可以输入一个现有名称并获得正确的结果,但如果我输入一个不在字典中的名称,它不会返回任何内容并再次告诉我输入一个名称。

显然这只是一个示例,我知道它应该做什么,但为什么要这样做?

【问题讨论】:

  • 您的代码对我来说运行良好,后一个示例中的缩进是否与您尝试运行的内容相同?原来是这个问题。
  • 嗯,好问题。我专门为帖子重新输入了这个,所以缩进可能不一样。或者它是。当我开始尝试调整它时,我应该做的是创建另一个 git 分支,但我当然没有想到,直到为时已晚。过失。

标签: python python-3.x dictionary


【解决方案1】:
birthdays[name] = bday

这一行是第二个代码块之后的问题。您必须用新值更新字典中的值。在您的示例中,您将 bday 分配给应该已经在字典中的名称。

编辑:

birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}

while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        break

    if name in birthdays:
        print(name + ' birthday is ' + birthdays[name])
    else:
        print('I do not have birthday information for ' + name)
        print('What is their birthday?(ex: Apr 4): ')
        bday = input()
        birthdays.update({name: bday})
        print('Birthday database updated.')
        print(birthdays)

【讨论】:

  • 好吧,这很公平。但是我仍然对为什么书中的示例原始代码无法正常工作感到困惑。我担心我的环境可能有问题。
【解决方案2】:

我的钱在 else 块上,因为 if/else 的缩进与 if 不同。这是我能够重现您所描述的行为的唯一方法:

birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}

while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        break

    if name in birthdays:
        print(birthdays[name] + ' is the birthday of ' + name)
# Notice the else corresponds to "while", not the above "if".
else:
    print('I do not have birthday information for ' + name)
    print('What is their birthday?')
    bday = input()
    birthdays[name] = bday
    print('Birthday database updated.')

请注意,这实际上是有效的 Python,如果 while 在没有 break 语句的情况下退出,则将输入 else 子句。

例如:

while 1 != 1:
    pass
else:
    print("Got to else.")

输出:

Got to else.

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2015-12-26
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2018-09-26
    • 2012-10-13
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多