-1

I made a calender to exercise my python skills. I run into an error with this code:

month_status = 1
month_Name = ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "Sebtember", "Oktober", "November", "Desember"]

for month in range(13):
    valuen = month+1
    print("Now is", valuen , "that is ", month_Name[month])
        if valuen == 1:
            if month_status == 1:
                for f in range(1,29):
                    print(f)
                month_status = 2
            else:
                for f in range(1,30):
                    print(f)
                month_status = 1
    # this elif should be executed first because it has 0 as the first array
        elif valuen == 0 or 2 or 4 or 6 or 7 or 9 or 11 :
            for i in range(1,32):
                print(i)
    # and the annoying fact is this second elif would not be executed
        elif valuen == 3 or 5 or 8 or 10 :
            for i in range(1,31):
                print(i)
        else:
            print("error")

Why does this happen?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102

2 Answers2

0

Change this:

valuen == 0 or 2 or 4 or 6 or 7 or 9 or 11

To this:

valuen in [0 , 2 , 4 , 6 , 7 , 9 , 11]

And this:

valuen == 3 or 5 or 8 or 10

To this:

valuen in [3 , 5 , 8 , 10]
0

It will not work because this line doesn't achieve what you have in mind:

 elif valuen == 0 or 2 or 4 or 6 or 7 or 9 or 11

Doing this means (0 or 2 or 4 or 6 or 7 or 9 or 11) will give this first non-zero (True) value. Example:

print(0 or 2 or 4 or 6 or 7 or 9 or 11)

will give 2

print(4 or 0 or 7 or 9 or 11)

will give 4

To achieve your purpose you should do this:

elif valuen == 0 or valuen == 2 or valuen == 4 or valuen == 6 or valuen == 7 or valuen == 9 or valuen == 11

This will work

Chandler Bong
  • 461
  • 1
  • 3
  • 18