【发布时间】:2021-09-11 18:07:52
【问题描述】:
我刚开始学习 python,我用if 语句编写了一些代码,我认为代码中有太多elif 语句,所以我想知道是否有任何方法可以缩短代码。
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")
【问题讨论】: