python - 打印未执行
问题描述
最后几行的 print() 没有执行,我尝试简单地缩进它们,但是变量 [age_1] 将未定义。这是我的代码:
from datetime import datetime
birth_year = input("When were you born? ")
time = datetime.now()
year = int(time.strftime("%Y"))
age = year - int(birth_year)
print(age)
answer = input(f"So, you're {age}, yes? ")
if answer == "No" or answer == "no" or answer == "NO":
other_age = input("I am sorry, perhaps I miscalculated, can you input your birth year once more? ")
if age == other_age:
# Problem part
print(f"""
Hold on...You were lying about me being incorrect!
So you are {age}!
""")
当我运行代码时,其他的工作正常,但最后一部分的打印语句在我运行代码时没有显示
解决方案
other_age
是字符串输入,即出生年份。
所以在这里你需要像第一次输入一样再次计算输入后的年龄,你的条件将起作用并成功打印输出。
输入后添加other_age
:
other_age = year - int(other_age)
推荐阅读
- python - 如何设置 VS Code for Python 以正确启动/调试单个文件,从而尊重相对导入?
- android - 如何在多 abi QT 构建中引用外部库
- python - 使用 python 和 fetchall() 从数据库匹配查询时面临问题
- python - 使用 Selenium 和不同文本部分之间的空格抓取类的文本
- javascript - 如何将复杂模型中的输入值从视图传递到控制器?
- python - 如何在不考虑 div 索引的情况下识别 XPath?
- c# - Angular:在 HTML 页面中创建变量
- css - bootstrap 5中带有标签的自动边距
- c - 如何从 malloc 中删除单个“块”并使用正确的新内存重新分配新堆栈?
- python - 在 django3.2 下使用 autocomplete_fields 字段时出现以下问题