While、For循环及if条件判断

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: egon
password: 123
work_mons: 12
salary: 10000


            1 查询总工资
            2 查询用户身份
            3 退出登录
            
>>: 1
总工资是: 120000.0


            1 查询总工资
            2 查询用户身份
            3 退出登录
            
>>: 2
unkown 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值