首页 > 代码库 > python_day2作业

python_day2作业

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

技术分享
1 msg = hello egon 666
2 for i in msg[0:len(msg)]:
3     print(i)
View Code

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

技术分享
1 msg = hello egon 666
2 count =0
3 while count < len(msg):
4     print(msg[count])
5     count += 1
View Code

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

技术分享
1 msg = hello alex
2 print(msg.replace(alex,SB))   #str.replace()
View Code

4:msg=‘/etc/a.txt|365|get‘ 将该字符的文件名,文件大小,操作方法切割出来

技术分享
1 msg = /etc/a.txt|365|get
2 print(msg.split(|,))       #str.split()
View Code

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

技术分享
1 while True:
2     ret = input(please input cmd>>>)
3     if ret == ‘‘:
4         continue
5     ret = ret.strip()            #去除空格
6     print(执行该命令:%s %ret)
7     break
View Code

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

技术分享
1 while True:
2     user = input(请输入用户名:)
3     passwd = input(请输入密码:)
4     user = user.strip()
5     if user.isdigit() or  user == ‘‘:
6         continue
7     else:
8         print(恭喜登陆成功!)
9         break
View Code

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

技术分享
1 while True:
2     ret = input(请输入内容:)
3     if ret.startswith(alex):
4         ret += _SB
5         print(ret)
View Code

8.

两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、
每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者
月工资不为整数,则重新输入

技术分享

 1 tag = True
 2 while tag:
 3     user = input(请输入用户名:)
 4     passwd = input(请输入密码:)
 5     user = user.strip()
 6     passwd = passwd.strip()
 7     if user == ‘‘ or passwd == ‘‘:
 8         print(用户或密码输入错误,请从新输入)
 9         continue
10     else:
11         print(登陆成功!)
12         while tag:
13             mounth = (input(工作了几个月:))
14             mounth_mouey = (input(每月工资(整数):))
15             if mounth.isdigit() and mounth_mouey.isdigit():
16                 while tag:
17                     print(命令提示\n1.查询总工资\n2.查询用户身份\n3.退出)
18                     user_choose = (input(">>>"))
19                     if user_choose == 1:
20                         total_money = int(mounth) * int(mounth_mouey)
21                         print(total_money,)
22                     elif user_choose == 2:
23                         if user == alex:
24                             print(super user)
25                         elif user == yuanhao or user == wupeiqi:
26                             print(normal user)
27                         else:
28                             print(unkown user)
29                     elif user_choose == 3:
30                         tag = False
31             else:
32                 print(月份与工资请输入整数)
33                 continue

View Code

python_day2作业