TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

30 views
Skip to first unread message

Lee Tim

unread,
Nov 7, 2018, 4:26:31 AM11/7/18
to python.tw
def IsYearLeap(yr):
if yr % 4 == 0 and (yr % 100 !=0 or yr % 400 == 0):
return True
return False

yr = print(int(input("The year is:")))
print(IsYearLeap(yr))

File "/Users/timlee/Documents/Python/Test.py", line 2, in IsYearLeap if yr % 4 == 0 and (yr % 100 !=0 or yr % 400 == 0): TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

我不獨太明白為什麼會有這個Error,請求各位大大指教這個傻傻的問題 ~~謝謝

Albin Sun

unread,
Nov 7, 2018, 7:50:42 PM11/7/18
to python.tw
yr = print(....)

are u sure?

Dincht Wu

unread,
Nov 8, 2018, 1:38:57 AM11/8/18
to python.tw
test = print(int(input("The year is:")))
print(type(test))
The year is:123
123
<class 'NoneType'>

以上測試的結果,yr不是int,而是NoneType,因為yr指派的是print(...)
你可能要改為
yr = int(input("The year is:"))
print(IsYearLeap(yr))


Lee Tim於 2018年11月7日星期三 UTC+8下午5時26分31秒寫道:
Reply all
Reply to author
Forward
0 new messages