Python基础第二天
一、内容

二、练习
练习1
题目:已知msg='hello knight 666'编写for循环,利用索引遍历出每一个字符
图示:

代码:
msg = 'hello knight 666'
msg_len = len(msg)
for i in range(msg_len):
print(msg[i])
输出结果:
h
e
l
l
o k
n
i
g
h
t 6
6
6
练习2
题目:已知msg='hello knight 666'编写for循环,利用索引遍历出每一个字符
图示:

代码:
msg = 'hello knight 666'
count = 0
while True:
print(msg[count])
count += 1
if count == len(msg):
break
输出结果:
h
e
l
l
o k
n
i
g
h
t 6
6
6
练习3
题目:已知变量msg='hello qishi',将msg中的qishi替换成knight
代码:
msg='hello qishi'
msg_new = msg.replace('qishi','knight')
print(msg_new)
输出结果:
hello knight
练习4
题目:已知 msg='/etc/a.txt|365|get' 将该字符的文件名,文件大小,操作方法切割出来。
代码:
msg ='/etc/a.txt|365|get'
msg_new = msg.split('|')
print('文件名:',msg_new[0])
print('文件大小:',msg_new[1])
print('操作方法:',msg_new[2])
输出结果:
文件名: /etc/a.txt
文件大小: 365
操作方法: get
练习5
题目:编写while循环,要求用户输入命令,如果命令为空,则继续输入
图示:

代码:
count = 0
while True:
user_input = input('Please enter command:').strip()
if not user_input:
continue
print('The cmd is %s'%user_input)
练习6
题目:编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
图示:

代码:
while True:
user = input('Please enter username:').strip()
password = input('Please enter password:')
if not user or user.isdigit():
print('The username you entered is blank, please re-enter')
continue
print('Welcome %s'%user)
break
练习7
题目:编写while循环,让用户输入内容,判断输入的内容以knight开头的,则将该字符串加上_successful结尾
图示:

代码1:
while True:
user = input('Please enter:').strip()
if user.startswith('knight'):
print(user+'_successful')
代码2:
while True:
user = input('Please enter:').strip()
if user.startswith('knight'):
print('%s%s'%(user,'_successful'))
练习8
题目:
(1)两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者月工资不为整数,则重新输入
(2)认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为knight则打印super user,如果用户名为tangbao或者zhuozi则打印normal user,其余情况均打印unknown user),退出功能
(3)要求用户输入退出,则退出所有循环(使用tag的方式)
图示:

代码:
user_info = ['knight','zhuozi','tangbao']
tag = True
while tag:
username = input('Please enter username:').strip()
password = input('Please enter password:')
if not username or not password:
print('Your account or password is blank, please re-enter')
continue
month = input('Please enter the month:').strip()
salary = input('Please enter the salary:').strip()
if not month.isdigit() or not salary.isdigit():
print('The month or salary you enter must be an integer.')
if username == 'root' and password == '123456':
print('Login successfully!')
while tag:
print('请选择以下功能\n1、查询用户功能\n2、查询总工资功能\n3、退出')
cmd = input('Please select function:').strip()
if cmd == '1':
user = input('Please enter username:').strip()
if user == 'knight':
print('super user')
elif user == 'tangbao' or user == 'zhuozi':
print('normal user')
else:
print('known user')
elif cmd =='2':
print('Your total salary is %s'%(int(month)*int(salary)))
elif cmd =='3':
print('Goodbye!')
tag = False
else:
print('Invalid command,please try again!')
else:
print('Sorry,your account or password is incorrect,please try again!')
三、英语
1、invalid
[ˈɪnvəlɪd;ɪnˈvælɪd] adj.无效的
2、item
['aɪtəm] n.项目
3、incorrect
[,ɪnkə'rɛkt] adj. 错误的,不正确的
4、range
[rendʒ] n. 范围
5、parameter
[pə'ræmɪtɚ] n. 参数
6、error
['ɛrɚ] n.错误
7、step
[stɛp] n.步
8、setting
['sɛtɪŋ] v.设定
9、key
[kiː] n.键
10、value
['vælju] n.值
11、increase
['ɪnkris] v.增加
12、decrease
[dɪ'kris] v.减少
13、script
[skrɪpt] n.脚本
14、font
[fɑnt] n. 字体
15、type
[taɪp] n.类型
16、strip
[strɪp] vt.剥离;脱去
17、split
[splɪt] vt.分割
18、count
[kaʊnt] vt.计数
19、continue
[kən'tɪnju] vi. 继续
20、break
[brek] vi.打断
Python基础第二天的更多相关文章
- python基础-第二篇-基本数据类型
一.运算符 1.算数运算: 算数运算符相信大家都不陌生吧,尤其是加减乘除,好!那我就带着大家看看最后三个,这三个到底是干什么玩意的? %,取两数相除的余数,看图: **,x的多少次幂,看图: //,取 ...
- Python基础第二篇
一.三元运算 if True: name='a' else: name='b' #上面的代码用三元运算表示: name="a" if True else "b" ...
- Python基础 第二天
1.http://www.cnblogs.com/beer/p/5672678.html requests和beautifulsoup
- Python基础第二课
字符串(引号):四种表达方式 n1 = "我是" n1 = '我是' n1 = """我是""" n1 = '" ...
- python基础(二)----数据类型
Python基础第二章 二进制 字符编码 基本数据类型-数字 基本数据类型-字符串 基本数据类型-列表 基本数据类型-元组 可变.不可变数据类型和hash 基本数据类型-字典 基本数据类型-集合 二进 ...
- Python之路【第二篇】:Python基础
参考链接:老师 BLOG : http://www.cnblogs.com/wupeiqi/articles/4906230.html 入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈 ...
- Python之路【第二篇】:Python基础(一)
一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1: name = 'wupeiqi' print name 下面的结论对吗? ...
- 第二章:python基础,数据类型
"""第二章:python基础,数据类型2.1 变量及身份运算补充2.2 二进制数2.3 字符编码每8位所占的空间位一个比特,这是计算机中最小的表示单位.每8个比特组成一 ...
- 《python基础教程(第二版)》学习笔记 文件和素材(第11章)
<python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...
随机推荐
- mycat——未完成
https://www.cnblogs.com/joylee/p/7513038.html https://blog.csdn.net/wrs120/article/details/80417345 ...
- 如何转成libsvm支持的数据格式并做回归分析
本次实验的数据是来自老师给的2006-2008年的日期,24小时的温度.电力负荷数据,以及2009年的日期,24小时的温度数据,目的是预测2009年每天24小时的电力负荷,实验数据本文不予给出. 用l ...
- 关于No Spring WebApplicationInitializer types detected on classpath的提示,tomcat 卡主
No Spring WebApplicationInitializer types detected on classpath 下一句:Initializing Spring root WebAppl ...
- 慕课笔记利用css进行布局【三列布局】
三个div中间自适应,两侧固定大小 <html> <head> <title>三列布局</title> <style type="tex ...
- Linux环境下使用VSCode编译makefile文件的注意事项
Linux环境下使用VSCode编译makefile文件的注意事项 首先安装C/C++的两个依赖 在debug,launch会自动的生成下方的launch.json launch.json { // ...
- Linux清除arp缓存
arp缓存就是IP地址和MAC地址关系缓存列表.在Windows下 arp -d [$ip] 不指定IP地址时清除所有arp缓存.在Linux下 arp -d $ip 必须指定IP地址才能执行这条命令 ...
- Chrome new features
Chrome new features copy fetch url fetch("http://10.1.5.202/deploy/http/send/svnuser", { & ...
- 2018/2/16 解析Logback的AppenderBase源码,并举一反三的实现Logback扩展功能的思路,以及它的实际业务应用场景
在学习Logback的过程中,知道了它有一个可以将日志往第三方数据源写的功能,这个功能的实现就是这个AppenderBase类,不禁想看看它的源码. 下面是AppenderBase类的所有子类(也就是 ...
- 一个Java开发的Python之路----------------(一)
最近开始学习Python了,主要是因为现在在给海航通过JAVA写CMDB运维管理平台,我就是作为唯一一个坐在运维屋里的开发,又当爹,又当妈,前端,后台,测试,设计,需求, 发布,统统一把抓!!在Git ...
- Linux下汇编语言学习笔记36 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...