为什么我的if语句后没有出现我的打印声明?

时间:2016-12-10 01:07:05

标签: python python-3.x

我正在学习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)

然而,它永远不会打印出来。我做错了什么?

1 个答案:

答案 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)