![](/img/trans.png)
[英]Why isn't my if statement executing its print statement Python 2.7.11
[英]Why isn't my print statement showing up after my if statement?
我正在學習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)
但是,它永遠不會打印出來。 我究竟做錯了什么?
正如其他人指出的那樣,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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.