Using modulus operator in a python conditional statement -
When I run the code, I get an error: all the arguments that were not changed during string formatting
year = raw_input ("Please enter a year") if the year% 4 == 0 & amp; Amp; Year% 100! = 0: Print ("{0} is a leap year", the year) Elife year% 4 == 0 & amp; Amp; Year% 100 == 0 & amp; Amp; Year% 400 == 0: print ("{0} is a leap year", the year) Other: Print ("{0 leap year is not year", year)
Because raw_input
returns a string, line
year% 4
An operator is trying to do that, which shares the same operator %
as modulus.
You have to convert your input to an integer, together with int ()
.
In addition, as mentioned, and
, no & amp; Amp; Amp; And
such as C.
Such:
While correct: # Prompt the user until they give a valid integer. Try the year = raw_input ("Please enter one year"): Exclude year = interval (year) ValueError: pass (year% 4 == 0) and (year% 100! = 0): print "{0} A leap year ". Format (year) Eleph (year% 4 == 0) and (year% 100 == 0) and (year% 400 == 0): Print "{0} is a leap year". Format (year) Other: Print "{0} is not a leap year". Note the correct string formatting by using format (year)
format ()
.
Comments
Post a Comment