首页 > 解决方案 > 打印未执行

问题描述

最后几行的 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}!
    """)

当我运行代码时,其他的工作正常,但最后一部分的打印语句在我运行代码时没有显示

标签: python

解决方案


other_age是字符串输入,即出生年份。

所以在这里你需要像第一次输入一样再次计算输入后的年龄,你的条件将起作用并成功打印输出。

输入后添加other_age

other_age = year - int(other_age)


推荐阅读