pythonを勉強し始めたプログラミング初心者です。
現在の西暦をinput関数で入力して、その西暦がうるう年であるかを判定する関数を作成しました
python
1def current_year(): 2 x = int(input('現在の西暦を入力してください')) 3 return x 4 5def is_leapyear(x): 6 if (x %400 == 0 or (x %4 == 0 and x %100 == 0)): 7 print('西暦{}年はうるう年です'.format(x)) 8 else: 9 print('西暦{}年はうるう年ではありません'.format(x))
関数を呼び出すために以下のコードを入力すると
python
1current_year() 2is_leapyear(current_year)
次のようなエラーが発生しました。
python
1TypeError Traceback (most recent call last) 2Input In [22], in <cell line: 2>() 3 1 current_year() 4----> 2 is_leapyear(current_year) 5 6Input In [21], in is_leapyear(x) 7 5 def is_leapyear(x): 8----> 6 if (x %400 == 0 or (x %4 == 0 and x %100 == 0)): 9 7 print('西暦{}年はうるう年です'.format(x)) 10 8 else: 11 12TypeError: unsupported operand type(s) for %: 'function' and 'int'
変数xのデータ型は最初の関数でint型で定義しているのに、なぜこのようなエラーが発生するのかが分かりません。教えていただけると幸いです。
よろしくお願いいたします。
year = current_year()
is_leapyear(year)
回答2件
あなたの回答
tips
プレビュー