给大家推荐一个公众号:诗葵1931
里面的爱情诗歌很美
3.1 编写函数test(password, earning, age)用于检测输入错误。要求输入密码password第一个符号不能是数字,工资earnings的范围是0—20000,工作年龄的范围是18—70。若三项检查都通过则返回True。
代码:
input
def test(password, earning, age):
assert password[0] not in ['0','1','2','3','4','5','6','7','8','9']
assert int(earning)>=0 and int(earning)<=20000
assert int(age)>=18 and int(age)<=70
return True
print("password:")
password = input()
print("earning:")
earning = input()
print("age:")
age = input()
print(test(password, earning, age))
结果:
3.2 输入某年某月某日,判断这一天是这一年的第几天。
代码:
dat = input('请输入某年某月某日,格式为 yyyy-mm-dd :')
y = int(dat[0:4]) #获取年份
m = int(dat[5:7]) #获取月份
d = int(dat[8:]) #获取日
ly = False
if y%100 == 0: #若年份能被100整除
if y%400 == 0: #且能被400整除
ly = True #则是闰年
else:
ly = False
elif y%4 == 0: #其它情况下,若能被4整除
ly = True #则为闰年
else:
ly = False
if ly == True: #若为闰年,则2月份有29天
ms = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
ms = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]