While、For循环及if条件判断

xiaoxiao2021-02-28  77

1:msg='hello egon 666'编写for循环,利用索引遍历出每一个字符 msg = 'hello egon 666' for i in range(0,len(msg)): print('(%d)'%i,msg[i]) 2:msg='hello egon 666'编写while循环,利用索引遍历出每一个字符 msg = 'hello egon 666' i=0 while i < len(msg): print('(%d)' %i, msg[i]) i += 1 3:msg='hello alex'中的alex替换成SB msg='hello alex' print(msg.replace('alex','SB')) 4:msg='/etc/a.txt|365|get'将该字符的文件名,文件大小,操作方法切割出来 msg='/etc/a.txt|365|get' print(msg.split("|")) 5.编写while循环,要求用户输入命令,如果命令为空,则继续输入 while True : list=input('请输入命令:') if list: break

6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入

while True: user = input('Please enter username:') if user.isspace() or user.isdigit() or not user: continue else: password = input('Please enter password:') break

7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾

while True: a=input('请输入:') if a.startswith('alex'): a+='_SB' print(a) 8.1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者月工资不为整数,则重新输入2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi则打印normal user,其余情况均打印unkown user),退出功能3.要求用户输入退出,则退出所有循环(使用tag的方式)运行效果如下:user: egonpassword: 123work_mons: 12salary: 10000            1 查询总工资            2 查询用户身份            3 退出登录            >>: 1总工资是: 120000.0            1 查询总工资            2 查询用户身份            3 退出登录            >>: 2unkown user            1 查询总工资            2 查询用户身份            3 退出登录            >>: 3 tag = True while tag: user = input('user:') if user.isspace() and not user: continue pwd = input('passwd:') if pwd.isspace() or not pwd: continue month = input('work_months:') if month.isdigit(): money = input('money:') else: continue if money.isdigit(): while tag: print('1、查询总工资\n2、查询用户身份\n3、退出登录') inp = input('>>:') if inp == '1': print('总工资:%s' %money) elif inp == '2': print(user) if user == 'alex': print('superuser') elif user == 'yuanhao' or user == 'wupeiqi': print('normaluser') else: print('unkownuser') elif inp == '3': tag = False pass else: continue
转载请注明原文地址: https://www.6miu.com/read-67877.html

最新回复(0)