函数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)

最新文章

  1. PHP中如何在数组中随机抽取n个数据的值 - array_rand()?
  2. MYSQL中关于日期处理的函数
  3. jquery时间倒计时
  4. vmware, failed to lock the file
  5. storm环境搭建
  6. iOS 本地加载html登陆页面
  7. yii2 html下拉框
  8. hdu-5586 Sum(dp)
  9. Sharepoint 2013 安装部署系列篇 第三篇 -- 安装和配置网络负载均衡在前端web服务器
  10. GUI动态创建button
  11. JAVASCRIPT 框架&gt;&gt;
  12. asp 正则替换函数
  13. IOS之按钮控件--Button全解析及使用 分类: ios技术 2015-01-17 17:09 169人阅读 评论(0) 收藏
  14. Java中的增强 for 循环 foreach
  15. MySql准备工作
  16. maven项目管理
  17. C#使用Selenium+PhantomJS抓取数据
  18. Java 利用 UUID 生成唯一性 ID 示例代码
  19. centos7配置vsftpd服务器
  20. cocos2dx 3.x ccPositionTextureColor_vert与ccPositionTextureColor_noMVP_vert

热门文章

  1. 转载-【深度学习】深入理解Batch Normalization批标准化
  2. 多媒体文件嵌入HTML中自动转码工具
  3. iOS - 验证输入的是否是正确的身份证号码和手机号码
  4. Vuex有哪些作用
  5. jsonpath 求和
  6. LeetCode:灯泡开关2
  7. Consul实现服务治理1
  8. B. Batch Sort
  9. Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)
  10. 关于一次性的数据输入,excel字符串连接保存到服务器还是CRUD?