day14 6.7hw

1:编写for循环,利用索引遍历出每一个字符


msg='hello egon 666'

 一般做法:

msg = 'hello egon 666' print([i for i in msg])
xxxxxxxxxx
2
 
1
msg = 'hello egon 666'
2
print([i for i in msg])

1142847-20170713153758384-314125913.png

符合题意的做法:

msg = 'hello egon 666' for i in range(len(msg)): print(msg[i],end=' ')
msg = 'hello egon 666' for i in range(len(msg)): print(msg[i],end=' ')
x
1
msg = 'hello egon 666'
2
for i in range(len(msg)):
3
    print(msg[i],end=' ')
 1142847-20170713153759556-1834618012.png
 

2:编写while循环,利用索引遍历出每一个字符

msg='hello egon 666'

msg = 'hello egon 666' msg_l = 0 while msg_l<len(msg): print(msg[msg_l],end=' ') msg_l += 1

msg = 'hello egon 666' msg_l = 0 while msg_l<len(msg): print(msg[msg_l],end=' ') msg_l += 1
x
1
msg = 'hello egon 666'
2
msg_l = 0
3
while msg_l<len(msg):
4
    print(msg[msg_l],end=' ')
5
    msg_l += 1

1142847-20170713153800197-359423623.png

 

3:

msg='hello alex'中的alex替换成SB

msg = 'hello alex' msg.replace('alex','SB') print(msg)

xxxxxxxxxx
4
 
1
msg = 'hello alex'
2
msg.replace('alex','SB')
3
print(msg)
4
 
         

 1142847-20170713153800790-326077904.png

 

4:

msg='/etc/a.txt|365|get'

将该字符的文件名,文件大小,操作方法切割出来


①正则

import re msg='/etc/a.txt|365|get' ret =re.findall(r'\w+\.?\w+',msg) print(ret)

import re msg='/etc/a.txt|365|get' ret =re.findall(r'\w+\.?\w+',msg) print(ret)
x
1
import re
2
msg='/etc/a.txt|365|get'
3
ret =re.findall(r'\w+\.?\w+',msg)
4
print(ret)
5
 
         

1142847-20170713153801478-341711517.png

②切片

1142847-20170713153802493-1284173997.png
  1142847-20170713153804728-1604708597.png
 


 

5.编写while循环,要求用户输入命令,如果命令为空,则继续输入

while True: cmd = input('cmd : ') if cmd: break

while True: cmd = input('cmd : ') if cmd: break
x
1
while True:
2
    cmd = input('cmd : ')
3
    if cmd:
4
        break



 

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

while True: name = input('name :') password = input('password :') if name and me.isdigit() == False: break else: continue

while True: name = input('name :') password = input('password :') if name and me.isdigit() == False: break else: continue
x
1
while True:
2
    name = input('name :')
3
    password = input('password :')
4
    if name and me.isdigit() == False:
5
        break
6
    else:
7
        continue

1142847-20170713153805759-1229062072.png

 


 

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

while True: str = input('请输入内容 :') if str.startswith('alex'): str += '_SB' print(str)

xxxxxxxxxx
5
 
1
while True:
2
    str = input('请输入内容 :')
3
    if str.startswith('alex'):
4
        str += '_SB'
5
    print(str)

 1142847-20170713153806493-1532432264.png


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: name = input('user: ') password = input('passeord: ') month = (input('work_mons: ')) salary =(input('salary: ')) if password !=''and name !='' and month.isdigit() ==True and salary.isdigit() == True: print('login successful') while tag: print('**************请选择命令*******************') print('1.查询总工资') print('2.查询用户身份') print('3.退出') print('******************************************') s = input('输入命令代码>>: ') if s =='1': print( '总工资 = ',(int(salary)*int(month))) elif s == '2': if name == 'alex': print('super user') elif name == 'yuanhao'or name =='wupeiqi': print('normal user') else : print('unkown user') elif s =='3': tag = False else: print('输入错误')
x
 
1
tag = True
2
while tag:
3
    name = input('user: ')
4
    password = input('passeord: ')
5
    month = (input('work_mons: '))
6
    salary =(input('salary: '))
7
 
          
8
    if password !=''and name !='' and month.isdigit() ==True and salary.isdigit() == True:
9
        print('login successful')
10
 
          
11
        while tag:
12
            print('**************请选择命令*******************')
13
            print('1.查询总工资')
14
            print('2.查询用户身份')
15
            print('3.退出')
16
            print('******************************************')
17
            s = input('输入命令代码>>: ')
18
 
          
19
            if s =='1':
20
                print( '总工资 = ',(int(salary)*int(month)))
21
 
          
22
            elif s == '2':
23
                if name == 'alex':
24
                    print('super user')
25
                elif name == 'yuanhao'or name =='wupeiqi':
26
                    print('normal user')
27
                else :
28
                    print('unkown user')
29
 
          
30
            elif s =='3':
31
                tag = False
32
 
          
33
            else:
34
                print('输入错误')
1142847-20170713153807900-781094136.png
 




转载于:https://www.cnblogs.com/hsddon/p/7160850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值