第七章 用户输入与while循环
1.用户输入:input()函数
(1)input()函数的工作原理
input()会让程序暂停运行,等待用户输入一些文本。
获取用户输入后,可将其存储在一个变量中。
message = input('write somthing:')
*:input()函数会将读入的内容全默认为字符串,
若要进行数值运算,则需进行格式转换:int()、float()
(2)input()函数的编写小技巧
若提示语超过一行,则可存储在变量中,再调用input()函数。
下面的代码可以借鉴:
message = 'a line.'
message += '\na new line'
lines = input(message)
2.while循环
while循环执行时,当测试条件满足,会一直执行,直到测试条件不满足,才结束。
(1)while循环实现列表之间移动元素

与前一章写到的if语句条件判断一样,
非空列表的布尔值为True,空列表的布尔值为False。
(2)while循环实现删除包含特定值的所有列表元素

(3)while循环实现使用用户输入来填充字典
不多说了,直接上图


忽然发现单词拼写错误。。。。见谅!!!!
3.循环流程的控制:continnue、break
(1)break:立即跳出整个循环
(2)continnue:跳过本循环,回到循环开头的条件判断
循环一定要有结束或者中断的设置 ,否则就会出现死循环!
这个要尽量避免!!!
网友评论