1.用户输入月份,判断这个月是哪个季节
month = int(input('Month:'))
if month in [3,4,5]:
print('春季')
elif month in [6,7,8]:
print('夏季')
elif month in [9,10,11]:
print('秋季')
elif month in [12,1,2]:
print('冬季')
else:
print('输入不合法')
2.假定有下面的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘apple’]
输出结果为:'I have fentiao, fendai, fensi and apple.'
names = ['fentiao','fendai','fensi','apple'
print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])
3.输入年月日,求出这一日是这一年的多少天
time = input('请输入年月日,YYYY-MM-DD:')
date = time.split('-')
year= int(date[0])
month = int(date[1])
day = int(date[2])
num = 0
li = [31,28,31,30,31,30,31,31,30,31,30,31]
if (year % 4 == 0 and year % 100 != 0) or (year %400 == 0):
li[1]=29
for i in range(12):
if month > i+1:
num += li[i]
else:
num += day
break
print('这是%d年的%d天' %(year,num))
4.用户管理系统
1.系统里面有多个用户,用户的信息目前保存在列表里面
users = [‘root’,‘westos’]
passwd = [‘123’,‘456’]
2.用户登陆(判断用户登陆是否成功
1).判断用户是否存在
2).如果存在
1).判断用户密码是否正确
如果正确,登陆成功,推出循环
如果密码不正确,重新登陆,总共有三次机会登陆
3).如果用户不存在
重新登陆,总共有三次机会
第一种方法:
users = ['root', 'westos']
password = ['123', '456']
trycount = 0
while trycount < 3:
name = input('用户名:')
passwd = input('密码:')
trycount += 1
if name in users :
index = users.index(name)
passwds = password[index]
if passwd == passwds:
print('登录成功')
break
else:
print('%s登录失败,密码错误' %name)
print('您还剩余%d次机会' % (3 - trycount))
else:
print('%s用户不存在' %name)
print('您还剩余%d次机会' % (3 - trycount))
else:
print('登录次数超过三次,请稍后登录')
第二种方法:
users=['root','westos']
passwd=['123','456']
print('---------------登陆系统---------------')
usertime=1
passtime=1
while usertime<=3:
a= input('请输入您的用户名:')
if a in users:
while passtime<=3:
if input('请输入您的密码:') == passwd[(users.index(a))]:
print('登陆成功')
exit()
else:
print('密码错误')
passtime+=1
print('密码输入错误次数已达限制,程序退出')
exit()
else:
print('用户不存在:')
usertime+=1
print('用户输入错误次数已达限制,程序退出')