If you include else in python, you get an error.I don't know how to deal with it at all.

Asked 2 years ago, Updated 2 years ago, 274 views

Regarding the program like this, the else seems to be strange.When I try to run it in the terminal, it says SyntaxError: invalid syntax.Please let me know the solution.

a=input("Enter a number.a = "
a = int(a)

if a%4 == 0 and a%100! = 0:
    print("This is a leap year.")

    if a %400 == 0:
        print("Leap year".Congratulations.")

        else:
            print("Not leap year")

python

2022-09-30 22:01

2 Answers

if-else indentation is not aligned.

modified:

 if a %400 == 0:
        print("Leap year".Congratulations.")

    else:
        print("Not leap year")


2022-09-30 22:01

Cubick's answer is correct as a solution to the error being questioned.However, I guess that the logic that you want to code is to determine leap year.In that case, even if there are no errors in the syntax of the indentation, it is correct.If it's a question, it's an answer, but here are some examples of the correct logic and syntax.

if a%400==0 or (a%4==0 and a%100!=0):
    print("Leap year".")
else:
    print("Not leap year")


2022-09-30 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.