用户输入和while循环
函数input()的工作原理
message=input('Tell me something,and I will repeat it back to you:')
print(message)
编写清晰的程序
#有时,提示可能超过一行,可将提示存储在一个变量中,再将该变量传递给函数input()。
prompt='If you tell us who you are,we can personalize the message you see.'
prompt+='\nWhat is your first name?'
#第一行将消息的前半部分存储进变量
#第二行运算符‘’+=‘在存储在变量中的字符串末尾附加一个字符串
name=input(prompt)
print('\nHello, ' + name + '!')
使用while循环
current_number=1
while current_number<=5:
print(current_number)
current_number+=1
使用标志
active=True #变量active设置成True,让程序最初处于活动状态。
while active: #只要变量为True,循环将继续进行
message=input('>>:')
if message=='quit':
active=False #输入‘quit’,变量设置为False,导致while不再循环
else:
print(message)
使用break退出循环
active=True
while active:
message=input('>>:')
if message=='quit':
break
else:
print(message)
再循环中使用continue
current_number=0
while current_number<10:
current_number+=1
if current_number%2==0:
continue # 变量是偶数,执行continue语句,忽略余下代码,返回循环的开头
print(current_number)
打印出来
1
3
5
7
9
在列表之间移动元素
unconfirmed_users=['alice','brian','candace',]
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
confirmed_users.append(current_user)
for confirmed_user in confirmed_users:
print(confirmed_user.title())
删除包含特定值的所有列表
sandwich_orders=['jjc','wcx','bbb','pastrami','pastrami','pastrami']
finished_sandwichs=[]
print('pastrami is finished')
while 'pastrami' in sandwich_orders: #删除特定值
sandwich_orders.remove('pastrami')
print(sandwich_orders)
for sandwich_order in sandwich_orders:
finished_sandwichs.append(sandwich_order)
print('I made your ' + sandwich_order + ' sandwich')
for finished_sandwich in finished_sandwichs:
print(finished_sandwich)
用户输入和while循环的更多相关文章
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- 用户输入与while循环
函数input()的工作原理: 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中 测试input()功能-- #!/usr/bin/env python#fi ...
- python从入门到实践-7章用户输入和while循环
#!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...
- Python:从入门到实践--第七章--用户输入和while循环-练习
#1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...
- 《Python编程从入门到实践》_第七章_用户输入和whlie循环
函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...
- python的用户输入和while循环
1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求 ...
- 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环
7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...
- 第七章 用户输入和while 循环
7.1 创建多行字符串的方式: 01 prompt="if you tell me who you are, we can personalize the message you see.& ...
- 用户输入和while 循环
input 工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中. message = input("need to input ...
随机推荐
- matplotlib画线(2)
这篇随笔是matplotlib画线的补充>>> #nocl参数控制图例中有几列,>>> import numpy as np>>> import ...
- List Control控件中及时捕获checkbox被选中的消息的解决方案
转自:http://blog.csdn.net/vycode/article/details/7345073 我的功能需求是:用户可以在List Control里添加item,当无选项被选中(即Che ...
- c#封装dll
https://www.cnblogs.com/xingboy/p/10287425.html
- Java反射机制调用对象的方法 —— 将一个对象的属性值赋值给另一个对象的属性
模拟一个场景: 众所周知,EasyExcel导出Excel文档是依赖于注解完成的,在实体类需要导出的属性上面加上注解,导出的时候会自动识别该属性. 假如我们现在需要导出用户的信息,又不想污染原本的实体 ...
- Python爬虫之设置selenium webdriver等待
Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...
- SpringMVC入门 bug集锦X3和SSM原始整合
- xml布局文件
https://blog.csdn.net/u013475386/article/details/44339035 gravity写在容器中中 layout_gravity写在控件中 layout_m ...
- CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙
嗯~~,好题... 用线段树维护区间gcd,按如下法则递归:(记题目中猜测的那个数为x,改动次数为tot) 1.若子区间的gcd是x的倍数,不递归: 2.若子区间的gcd是x的倍数,且没有递归到叶子结 ...
- CoreCLR
CoreCLR 在这篇中我将讲述GC Collector内部的实现, 这是CoreCLR中除了JIT以外最复杂部分,下面一些概念目前尚未有公开的文档和书籍讲到. 为了分析这部分我花了一个多月的时间,期 ...
- 看懂物联网fr
看懂物联网 2015-10-11 物联网世界 1.第三次IT浪潮 互联网时代的特征是信息驱动了生产力,无论众包.订单式生产这些理论:还是B2C.O2O各类业务模式:归根结底,是信息优化了生产关 ...