1:编写for循环,利用索引遍历出每一个字符    msg = 'hello egon 666'
msg = 'hello egon 666'
i = 0
for i in range(0, len(msg)):
print(msg[i], end=',')
i += 1

  

2:编写while循环,利用索引遍历出每一个字符  msg = 'hello egon 666'
msg = 'hello egon 666'
i = 0
while i < len(msg):
print(msg[i], end=',')
i += 1

  

3:msg = 'hello alex'  中的alex替换成66
msg = 'hello alex'
print(msg.replace('alex', '66'))

  

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

msg = '/etc/a.txt|365|get'
print(msg.split('|'))

  

5.编写while循环,要求用户输入命令,如果命令为空,则继续输入
while True:
cmd = input('cmd>>> ')
if len(cmd) > 0:
print('命令为:', cmd)
break
6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
while True:
name = input('username: ')
if len(name) == 0:
continue
pwd = input('password:')
break
7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_66结尾
while True:
tips = input('tips: ')
if tips.startswith('alex'):
tips = tips.replace('alex', 'alex_66')
print(tips)
break

  

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

2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印superuser,如果用户名为yuanhao或者wupeiqi,则打印normaluser,其余情况均打印unkownuser,退出功能

3.要求用户输入退出,则退出所有循环(使用tag的方式)

tag = True
while tag:
user = input('username:')
pwd = input('password:')
work_mons = input('work_mons:')
salary = input('salary:')
if len(user) != 0 and len(pwd) != 0 and work_mons.isdigit() and salary.isdigit():
print('登录成功!')
print('1', '\n', '查询总工资')
print('2', '\n', '查询用户身份')
print('3', '\n', '退出登录')
print('登录成功,请选择!')
while tag:
i = input('>>>')
if i == '1':
w = int(work_mons)
sa = int(salary)
s = sa * w
print('总工资是:', s)
elif i == '2':
if user == 'alex':
print('superuser')
elif user == 'yuanhao' or user == 'wupeiqi':
print('normaluser')
else:
print('unknown user')
elif i == '3':
tag = False
else:
print('输入错误,请输入有效选项')
continue
else:
print('failure')

  

# 运行效果如下:
# 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

python3,循环,方法练习2的更多相关文章

  1. angular.foreach 循环方法使用指南

    angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...

  2. PHP foreach()跳出本次或当前循环与终止循环方法

    PHPforeach()跳出本次或当前循环与终止循环方法 PHP中用foreach()循环中,想要在循环的时候,当满足某个条件时,想 $arr = array('a','b','c','d','e') ...

  3. Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-]# pytho ...

  4. java中的循环方法(附带本人遇到的坑)

    java循环结构 顺序结构的程序语句只能 被执行一次.如果你要同样的操作执行多次,就需要使用循环结构. java中有三种主要的循环结构: 1.while 循环 2.do...while 循环 3.fo ...

  5. php循环方法实现先序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). <?php class Node { public $v ...

  6. 【转】【CentOS】【Python】Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  7. Python3 len()方法

    Python3 len()方法  Python3 字符串 描述 Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. 语法 len()方法语法: len( s ) 参数 s - ...

  8. JavaScript里的循环方法总结

    JavaScript诞生已经有20多年了,我们一直使用的用来循环一个数组的方法是这样的: for (var index = 0; index < myArray.length; index++) ...

  9. Python3 循环语句

    Python3 循环语句 转来的  很适合小白   感谢作者   Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...

  10. Javascript/jQuery关于JSON或数组集合的几种循环方法

    JavaScript遍历JSON或数组集合: /** * 根据json数据生成option树形控件 * 如果有children节点则自动生成树形数据 * @param {JSON} data * @p ...

随机推荐

  1. JMeter接口响应数据出现乱码的三种解决方法

    第一种方法: Content encoding设置为utf-8,若仍为乱码,请用方法2 图1 第二种方法: 修改bin文件夹下的jmeter.properties文件 搜索ISO,把“#sampler ...

  2. Jmeter使用时异常问题解决

    1.执行jmeter请求时,响应数据中出现乱码异常(如图) 解决方案: 打开E:\apache-jmeter-4\bin\jmeter.properries(jmeter安装目录),查找到语句行:#s ...

  3. Mac下安装pear库+phpDocumentor

    1. 首先安装pear: curl -o go-pear.php https://pear.php.net/go-pear.phar 看见这个就安装OK: % Total % Received % X ...

  4. mysqldump: Got error: 1135: Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug when trying to connect 解决办法

    在进行数据库备份的时候发现服务器报 mysqldump: Got error: 1135: Can't create a new thread (errno 11); if you are not o ...

  5. EasyUI 布局 - 动态添加标签页(Tabs)

    首先导入js <link rel="stylesheet" href="../js/easyui/themes/default/easyui.css"&g ...

  6. HDU 2131 Probability

    http://acm.hdu.edu.cn/showproblem.php?pid=2131 Problem Description Mickey is interested in probabili ...

  7. 利用vue-cli搭建项目后的目录结构

    npm install -g vue-cli vue init webpack my-project(项目名称)  后的目录结构: -----build         webpack配置相关 --- ...

  8. Angular 2018 All in One

    Angular 2018 https://cli.angular.io/ v7.0.6 https://angular.io/ https://angular.cn/ v7.0.4 $ npm i - ...

  9. 批处理中的IF详解

    在CMD使用IF /?打开IF的系统帮助会发现IF有3种基本的用法! 第一种用法:IF [NOT] ERRORLEVEL number command 这种用法现在很少用了,因为它需要使用到CHOIC ...

  10. 高性能服务器开发之C++定时器

    高性能服务器开发之C++定时器 来源: https://www.cnblogs.com/junye/p/5836552.html 写这篇文章前搜了下网上类似的文章,有很多,所以笔者的这篇文章就不对定时 ...