【问题标题】:How to loop back to the first line after invalid input? Python [duplicate]无效输入后如何循环回第一行? Python [重复]
【发布时间】:2021-09-15 15:45:48
【问题描述】:

我最近开始了我的 Python 课程。在获得无效输入后,我想再次允许用户输入(回到问题的第一行),因此我在 while 循环中添加了。但是,现在,当输入为“是”时,python 会打印出“我不明白”。我能知道代码有什么问题吗?我应该如何修复它?

这里是代码

print('Hi, I am your bot, James!')

while True:
    user_reply=input("Are you ready for today's activity? ")
    if user_reply.lower == 'yes':
        for_calculation()

    elif user_reply.lower() == 'no':
        while True:
            double_confirm=input('Are you sure? ')
            if double_confirm.lower() == 'yes':
                print('See you next time.')
                exit()
            elif double_confirm.lower() == 'no':
                for_calculation()
            else:
                print('I do not understand.')

    else:
        print('I do not understand.')

结果如下

Hi, I am your bot, James!
Are you ready for today's activity? yes
I do not understand.
Are you ready for today's activity? 

【问题讨论】:

  • if user_reply.lower() == 'yes':替换if user_reply.lower == 'yes':

标签: python


【解决方案1】:

if user_reply.lower == 'yes': 中你忘记了()

if user_reply.lower() == 'yes':

如果您执行user_reply.lower,您将不会获得user_reply 的小写内容,您将获得为您提供小写内容的函数本身() 非常重要,因为它们告诉 Python 不要只获取函数,而是实际调用它并给你结果。

【讨论】:

  • 虽然是重复的。
猜你喜欢
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多