首页 > 解决方案 > 无效输入后如何循环回第一行?Python

问题描述

我最近开始了我的 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? 

标签: python

解决方案


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

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

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


推荐阅读