作业一
写一个用户登录认证的程序,比如用户名是“Albert”,密码是“1”,请用户分别输入用户名和密码来认证
name = input('请输入用户名字:')
password = input('请输入密码:')
if name == 'Albert' and password == '1':
print('Albert login success')
else:
print('用户名或密码错误')
作业二
写一个打印用户权限的程序,请用户输入用户名来验证
Albert --> 超级管理员tom --> 普通管理员jack,rain --> 业务主管其他 --> 普通用户'
'''Albert --> 超级管理员tom --> 普通管理员jack,rain --> 业务主管其他 --> 普通用户'''
name = input('请输入用户名字:')
if name == 'Albert':
print('超级管理员')
elif name == 'tom':
print('普通管理员')
elif name == 'jack' or name == 'rain':
print('业务主管')
else:
print('普通用户')
练习三
# 方式一:
today = input('>>: ')
if today == 'Monday':
print('上班')
elif today == 'Tuesday':
print('上班')
elif today == 'Wednesday':
print('上班')
elif today == 'Thursday':
print('上班')
elif today == 'Friday':
print('上班')
elif today == 'Saturday':
print('出去浪')
elif today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ''')
# 方式二:
today = input('>>: ')
if today == 'Saturday' or today == 'Sunday':
print('出去浪')
elif today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' \
or today == 'Thursday' or today == 'Friday':
print('上班')
else:
print('''必须输入其中一种: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ''')
# 方式三:
today = input('>>: ')
if today in ['Saturday', 'Sunday']:
print('出去浪')
elif today in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']:
print('上班')
else:
print('''必须输入其中一种: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ''')
练习四
# 实现一:
name = 'Albert'password = '123'
while True:
inp_name = input('用户名: ')
inp_pwd = input('密码: ')
if inp_name == name and inp_pwd == password:
while True:
cmd = input('>>: ')
if not cmd: continue
if cmd == 'quit':
break
print('run <%s>' % cmd)
else:
print('用户名或密码错误')
continue
break
# 实现二:使用
tagname = 'Albert'
password = '123'
tag = True
while tag:
inp_name = input('用户名: ')
inp_pwd = input('密码: ')
if inp_name == name and inp_pwd == password:
while tag:
cmd = input('>>: ')
if not cmd: continue
if cmd == 'quit':
tag = False
continue
print('run <%s>' % cmd)
else:
print('用户名或密码错误')
练习五
Albert_age = 18
count = 0
while True:
if count == 3:
choice = input('继续(Y/N?)>>: ')
if choice == 'Y' or choice == 'y':
count = 0
else:
break
guess = int(input('>>: '))
if guess == Albert_age:
print('you got it')
break
count += 1
练习六
# 题目一
# 方式一
count = 1
while count <= 10:
if count == 7:
count += 1
continue
print(count)
count += 1
# 方式二
count = 1
while count <= 10:
if count != 7:
print(count)
count += 1
# 方式三
for i in range(1, 11):
if i == 7:
continue
print(i)
# 题目二
res = 0
count = 1
while count <= 100:
res += count
count += 1
print(res)
# 题目三
count = 1
while count <= 100:
if count % 2 != 0:
print(count)
count += 1
# 题目四
count = 1
while count <= 100:
if count % 2 == 0:
print(count)
count += 1
# 题目五
res = 0
count = 1
while count <= 99:
if count % 2 == 0:
res -= count
else:
res += count
count += 1print(res)
练习七
"""
分析
#max_level=5 *
#current_level=1,空格数=4,*号数=1 ***
#current_level=2,空格数=3,*号数=3 *****
#current_level=3,空格数=2,*号数=5 *******
#current_level=4,空格数=1,*号数=7*********
#current_level=5,空格数=0,*号数=9
#数学表达式空格数=max_level-current_level*号数=2*current_level-1
"""
max_level = 5
for current_level in range(1, max_level + 1):
for i in range(max_level - current_level):
print(' ', end='') # 在一行中连续打印多个空格
for j in range(2 * current_level - 1):
print('*', end='') # 在一行中连续打印多个空格
print()