python - 无效输入后如何循环回第一行?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?
解决方案
在if user_reply.lower == 'yes':
你忘记了()
:
if user_reply.lower() == 'yes':
如果你这样做user_reply.lower
了,你不会得到 的小写内容user_reply
,你会得到为你提供小写内容的函数本身。它们非常重要,()
因为它们告诉 Python 不要只是获取函数,而是实际调用它并给你结果。
推荐阅读
- python - 重复分隔符并提取它们之间的字符串
- javascript - 保存对 Firestore 的引用类型,但获取 [object object] 字符串
- python-3.x - 检查版本似乎不适用于 Python2.7
- java - Android应用程序未释放内存且未关闭
- sql - 使用一对多而不是多对多 sql
- perl - 如何在 perl 中删除 Data::Dumper 中每个唯一数字的标题名称?
- html - CSS 关键帧动画在桌面上表现如预期,但在移动浏览器上显示空白帧/生涩动画
- asp.net-core - 基于角色创建授权
- c++ - 如何在 C++ 中将 Int 转换为 String,反之亦然(不使用像 sstream 这样的库)
- python - 将数据从树莓派上的 python 脚本发送到 chrome 扩展可以访问的地方的最佳方法是什么?