我正在学习Python,并决定尝试“今年是什么时候?”事情。这就是我所拥有的:
from datetime import datetime
now = datetime.now()
currentyear = now.year
userinput = input("What year is it? ")
if userinput == currentyear:
print ("Correct! The year is %s") % (currentyear)
然而,它永远不会打印出来。我做错了什么?
答案 0 :(得分:3)
正如其他人所指出的,Python 3并没有隐含地评估输入。使用int()调用将解决问题。
from datetime import datetime
now = datetime.now()
currentyear = now.year
userinput = input("What year is it? ")
if int(userinput) == currentyear
print ("Correct! The year is %s") % (currentyear)