|
|
这是第四份作业,有点难度
year = int(input("请输入具体年份:"))
month = int(input("请输入具体月份:"))
if month == 2:
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year,"年2月有29天。")
else:
print(year,"年2月有28天。")
elif month in {1,3,5,7,,8,10,12}:
print(year,"年",month,"月有31天。")
elif month in {4,6,9,11}:
print(year,"年",month,"月有30天。")
这是月份具体天数的代码,有点难。
你好!你写的代码整体思路正确,但在判断大月的集合中有一个小错误:{1,3,5,7,,8,10,12} 里面多了一个逗号,应该改成 {1,3,5,7,8,10,12}。另外,为了更健壮,可以添加月份输入的验证(比如只允许1-12),但根据你的作业要求,我们先修正这个错误。
以下是修正后的代码:
- year = int(input("请输入具体年份:"))
- month = int(input("请输入具体月份:"))
- if month == 2:
- if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
- print(year, "年2月有29天。")
- else:
- print(year, "年2月有28天。")
- elif month in {1, 3, 5, 7, 8, 10, 12}:
- print(year, "年", month, "月有31天。")
- elif month in {4, 6, 9, 11}:
- print(year, "年", month, "月有30天。")
复制代码
代码解释:
1. 首先获取年份和月份输入,并转换为整数。
2. 如果月份是2月,判断闰年:闰年规则是能被4整除但不能被100整除,或者能被400整除。如果是闰年,2月有29天,否则28天。
3. 如果月份在集合 {1,3,5,7,8,10,12} 中,这些是大月,有31天。
4. 如果月份在集合 {4,6,9,11} 中,这些是小月,有30天。
你的代码逻辑清晰,只要修正那个小错误就能正常运行。继续加油!如果还有其他问题,随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|