python练习1

1. 输入年、月,输出本月有多少天。合理选择分支语句完成设计任务。
    输入样例1:2004 2
    输出结果1:本月29天
    输入样例2:2010 4
    输出结果2:本月30天

year = int(input('请输入年份:'))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print ('本年为闰年 ')
else:
    print ('本年为平年 ')
month = int(input('请输入月份:'))
if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)) and month == 2:
    print ('本月为29天!')
if (year % 400 != 0) and ((month == 1) or
 (month == 3) or (month == 5) or
 (month == 7) or (month == 8) or
 (month == 10) or (month == 12)):
    print ('本月为31天!')
elif (year % 400 != 0) and (month == 4 or month == 6 or month == 9 or month == 11):
    print('本月为30天!')
elif (year % 4 != 0) and month == 2:
    print ('本月为28天!')

python练习1
2.用 if 判断输入的值是否为空?如果为空,报错Error。

i = input('python: ')
if i == '':
    print ('Error')
else:
    print ('OK!')

python练习1
3. 根据用于指定月份,打印该月份所属的季节。
**提示: 3,4,5 春季 6,7,8 夏季  9,10,11 秋季 12, 1, 2 冬季

month = int(input('请输入月份: '))
if month == 3 or month == 4 or month == 5:
    print ('该月份为春季')
elif month == 6 or month == 7 or month == 8:
    print ('该月份为夏季')
elif month == 9 or month == 10 or month == 11:
    print ('该月份为秋季')
elif month == 12 or month == 1 or month == 2:
    print('该月份为冬季')

python练习1