samedi 11 septembre 2021

Is there a way to shorten if-elif-else statement in python?

I am just started to learn python and I did some code with an if statement and I think there are too many elif statements in the code so I was wondering if there is any way I can shorten the code.

from random import *
month = randrange(1, 13)
if month == 1:
    print("January")
elif month == 2:
    print("February")
elif month == 3:
    print("March")
elif month == 4:
    print("April")
elif month == 5:
    print("May")
elif month == 6:
    print("June")
elif month == 7:
    print("July")
elif month == 8:
    print("August")
elif month == 9:
   print("September")
elif month == 10:
   print("October")
elif month == 11:
    print("November")
else:
    print("December")

Aucun commentaire:

Enregistrer un commentaire