1. 程式人生 > >Python代碼

Python代碼

months ont 工資 elif 利用 msg -- ns3 所有

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代碼