2016-12-10 1 views
0

私はPythonを学んでいて、「何年だ?ここに私が持っているものがあります:if文の後にprintステートメントが表示されないのはなぜですか?

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) 

しかし、それは決して印刷されません。私は間違って何をしていますか?

+2

'if int(userinput)==現在の年月日または' userinput == str(現在の年) 'です。 – Abdou

+0

int(userinput)が動作するようです。ありがとう! –

答えて

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) 
+1

ありがとう!私はタグを気付かず、主に2.7を使用しているので、私はこの問題を認識していませんでした。 –

+0

@Abdou、done。 :) –

+1

よろしくお願いします。完了しました。 –

関連する問題