Python代码,,1:编写for循环,


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

msg=‘hello egon 666‘

x=‘hello   egon   666‘
num=len(x)
print(num)
for i in range(num):
a=x[i]
print(a,end=‘‘)
print(x.index(a),end=‘ ‘)
print()
print(x.index(‘ ‘))

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

msg=‘hello egon 666‘

#!/usr/bin/env python

x=‘hello egon 666‘
num=len(x)
print(num)

for i in range(num):
a=x[i]
print(a,end=‘‘)
print(x.index(a),end=‘ ‘)
i+=1

3:

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

#!/usr/bin/env python
x=‘hello alex‘
y=x.replace(‘alex‘,‘SB‘)
print(y)

4:

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

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

#!/usr/bin/env python
msg=‘/etc/a.txt|365|get‘
x=msg.split(‘|‘)
print(x)

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

#!/usr/bin/env python
tag=True
while tag:
name=input(‘input your ID:‘)

if name.strip()==‘‘ or name.isdigit()==True:
continue
word=input(‘input your password:‘)

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

#!/usr/bin/env python
#tag=True 如果需要全部退出可以考虑tag标志位
while True:
ins=input(‘input your instruction:‘)
ins1=ins.strip()
if ins==‘‘:
continue
else:
break

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

#!/usr/bin/env python
tag=True
while tag:
ins=input(‘input your details:‘)
s=ins.startswith(‘alex‘)
if s==True:
ins+=‘_SB‘
print(ins)
#ins=ins.rjust(1,‘_‘)
#ins=ins.rjust(1,‘S‘)
#ins=ins.rjust(1,‘B‘)
#print(ins3)

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

#!/usr/bin/env python
tag=True
while tag:
name=input(‘input your ID:‘)
if name.strip()==‘‘:
continue
password=input(‘input your password:‘)
if password.strip==‘‘:
continue
salary=input(‘input your salary:‘)
if salary.isdigit==False:
continue

months=input(‘input how many months u have worked:‘)
if months.isdigit==False:
continue


if name==‘tony‘ and password==‘123456‘:
while tag:
ins=input(‘please input your instruction:\n#1--->查询总工资\n#2--->输入用户名查询用户身份\n#3--->退出登录‘)
if ins==‘1‘:
months=int(months)
salary=int(salary)
money=salary*months
print(money)
elif ins==‘alex‘:
print(‘super user‘)
elif ins==‘egon‘:
print(‘normal user‘)
elif ins==‘3‘:
tag=False
else:
print(‘unknown instruction or user‘)

Python代码

评论关闭