python - 满足条件时如何停止循环?
问题描述
我是尝试使用 python 进行编码的新手。在编写一个简单的程序来计算年龄时,我遇到了一个错误,无论我多么努力,我似乎都无法修复。代码粘贴在下面。该程序会在我输入未来的一年时运行,但是当使用已经过去的一年时,代码会再次循环询问年份是否正确。任何帮助表示赞赏。
from datetime import date
current_year = (date.today().year)
print('What is your age?')
myAge = input()
print('future_year?')
your_age_in_a_future_year= input()
future_age= int(myAge)+ (int(your_age_in_a_future_year)) - int(current_year)
def process ():
if future_age > 0:
print(future_age)
else:
print('do you really want to go back in time?, enter "yes" or "no"')
answer = input()
if answer =='yes':
print (future_age)
if answer == 'no':
print ('cya')
while answer != 'yes' or 'no':
print ('enter correct response')
process ()
process()
解决方案
尝试
...
if future_age > 0:
print(future_age)
return
else:
print('do you really want to go back in time?, enter "yes" or "no"')
...
推荐阅读
- python - 在熊猫中通过多个条件选择数据框行
- sql - ORACLE 15 天分组结果
- python - 试图让 pygame 文档代码工作
- google-chrome - 量角器不支持铬开关`--host-resolver-rules`
- github - GitHub Actions:仅为某些拉取请求基础分支运行步骤
- angular - Angular Google Maps mapClick() 事件不起作用
- next.js - 如何从 Nextjs 中的 url 参数获取数据?
- google-apps-script - 无法通过 GS 发布 WebApp
- autobahn - crossbar 路由器在没有 docker 的情况下启动
- email - 确定电子邮件接收者是否为 Outlook / Office 365 以呈现可操作邮件