第一次学Python,敲的代码总是感觉怪怪的,又说不出哪里怪(ಥ﹏ಥ)

‌在控制台中输入一个月份,11:30


‌# 返回该月份的天数


‌# 1 3 5 7 8 10 12 ---> 31 天


‌# 4 6 9 11 ---> 30 天


‌# 2 ---> 28 天
代码:
str1=[12,10,8,7,5,3,1 ]
str2=[11,9,6,4]
str3=[2]
month=int(input("请输入一个月份:")
if month in str1:
print("31")
elif month in str2:
print("30")
else:
print("28")

month=int(input("请输入一个月份:") 最后少了个右括号)
if month in str1: 和 elif month in str2: 最后的:号写成了中文全角的,要改成英文半角的。

你题目的解答代码如下:

str1=[12,10,8,7,5,3,1 ]
str2=[11,9,6,4]
str3=[2]
month=int(input("请输入一个月份:"))  #最后少了个右括号)
if month in str1:  #    :号写成了中文全角的,要改成英文半角的。
    print("31")
elif month in str2:  #    :号写成了中文全角的,要改成英文半角的。
    print("30")
else:
    print("28")

img

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

用if else实现是可以的,或者这样

days = [31,28,31,30,31,30,31,31,30,31,30,31]

month = eval(input())
print(days[month-1])
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632