Python课程设计 第 4 / 52 篇
流程控制-参考答案
作业一
写一个用户登录认证的程序,比如用户名是“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('普通用户')
练习三
写一个根据当日日期来说明是否上班的程序,请用户输入日期来获取
如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪
# 方式一:
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
''')
练习四
写一个循环验证用户登录的程序,用户认证登录成功后,输入“q”退出程序,用户认证失败后重复让用户执行登录操作,当用户重复次数超过3次仍没有登录成功,则退出程序。
"""
写一个循环验证用户登录的程序,用户认证登录成功后,
输入“q”退出程序,用户认证失败后重复让用户执行登录操作,
当用户重复次数超过3次仍没有登录成功,则退出程序。
"""
username = 'albert'
password = '1'
count = 0
tag = True
while tag:
username_input = input('username>>:').strip()
password_input = input('password>>:').strip()
if count == 3:
break
if username_input == username and password_input == password:
print('login successfully')
while tag:
cmd = input('>>:').strip()
if cmd == 'q':
tag = False
break
else:
print('username or password is invalid')
count += 1
练习五
写一个用户猜年龄的游戏,允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序,如何猜对了,就直接退出。
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
练习六
按要求打印如下结果:
- 使用while循环输出1 2 3 4 5 6 8 9 10
- 求1-100的所有数的和
- 输出 1-100 内的所有奇数
- 输出 1-100 内的所有偶数
- 求1-2+3-4+5 … 99的所有数的和
# 题目一
# 方式一
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 += 1
print(res)
练习七
打印如下金字塔图形,下图为等腰三角形,上面一行内容永远比下面少两颗星星且位于下面一行的正上发(提示:用两层for loop):
*
***
*****
*******
*********
"""
分析
#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()