14 While、For循环及if条件判断
2017-06-08 01:32
447 查看
1:msg='hello egon 666'
编写for循环,利用索引遍历出每一个字符
2:msg='hello egon 666'
编写while循环,利用索引遍历出每一个字符
3:msg='hello alex'中的alex替换成SB
4:msg='/etc/a.txt|365|get'
将该字符的文件名,文件大小,操作方法切割出来
5.编写while循环,要求用户输入命令,如果命令为空,则继续输入
6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
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
编写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
相关文章推荐
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字(for、while、if、else、switch、case)。
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
- if-elif条件判断,while循环和for…in循环
- Python基础(条件判断和循环) if elif else for while break continue;
- Python3学习笔记【基础3--list,tuple,if条件判断,for/while循环,dict字典,set集合】
- python if条件判断、for循环和while循环
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- Linux shell 流程控制(条件if,循环for,while)
- 不使用乘除法,for,while,if,else,switch,case,条件判断语句(A?B:C) 实现:1+2+....+n
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Linux shell 流程控制(条件if,循环for,while)
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- PHP 不用求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句