input 工作原理

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中。

message = input("need to  input string ")
print(message) input() 接受一个参数作为提示,程序等待用户输入后,在用户回车确认后继续运行,输入存储在变量中 或者: promt = "hello plse input you name"
promt += "\nyou fist name " input(promt)

使用 int()来获取数值输入

使用函数input()时,Python将用户输入解读为字符串,将输入作为数字使用,使用函数init()

for循环

a = ['ks','dkd','jdjd',10,]
for xx in a:
print(xx)

求模运算

处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。

求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少

如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判断一个数是奇数还是偶数

number % 2 == 0:

while循环

current_number = 1
while current_number <= 5:
print( str(current_number) + "回合")
current_number += 1
print(current_number)

让用户选择何时退出

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
print(message)

break 退出循环

立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt) active = True
while active :
xx = input(prompt)
if xx == "quit" :
active = False
else :
print(xx)

break循环

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt) while True :
xx = input(prompt)
if xx == "quit" :
break
else :
print(xx)

循环中使用continue

返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环


current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number) 首先将current_number设置成了0,由于它小于10,Python进入while循环。进入循环后,我们以步长1的方式往上数 ,因此current_number为1。接下来,if语句检查current_number与2的求模运算结果。如果结果为0(意味着current_number可被2整除),就执行continue语句,
让Python忽略余下的代码,并返回到循环的开头。如果当前的数字不能被2整除,就执行循环中余下的代码,Python将这个数字打印出来

避免无限循环

while 循环处理列表和字典

for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环

unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print(current_user.title()) confirmed_users.append(current_user) print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())

删除包含特定值的所有列表元素

用函数remove()来删除列表中的特定值.

pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove("cat") print(pets)

总结

break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环,并直接开始下一轮循环。这两个语句通常都必须配合if语句使用。

使用

用户输入来填充字典

responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ") # 将答卷存储在字典中
responses[name] = response # 看看是否还有人要参与调查
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False # 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")

最新文章

  1. mfc学习之路--如何删除通过控件新增的变量
  2. JavaScript高级程序设计学习笔记--事件
  3. 使用Prism6 建立 Windows 10 通用程序.
  4. DSP using MATLAB 示例 Example3.19
  5. NeHe OpenGL教程 第十三课:图像字体
  6. 学习Linux第三天
  7. asp:第三平台登陆
  8. 解读Spring Ioc容器设计图
  9. shell编程的一些例子3
  10. d008: 求两数的整数商 和 商
  11. hdu-Common Subsequence
  12. React Image加载图片过大导致ListView滑动卡顿
  13. flask based on tornado
  14. Spring Boot配置加载顺序
  15. Mybatis类型转换介绍
  16. docker-compose.md
  17. ASP.NET js控制treeview中的checkbox实现单选功能
  18. tf.multiply()和tf.matmul()区别
  19. 【TCP/IP详解 卷一:协议】第十二章 广播和多播
  20. python2.0_s12_day9_事件驱动编程&amp;异步IO

热门文章

  1. Python Basics with Numpy
  2. 四、Kubernetes_V1.10集群部署-master-创建kubeconfig
  3. nodejs 和 js
  4. 2014年北京 happy matt friends(dp + 滚动数组优化)
  5. OLT设置vlan透传及ONU端口划vlan
  6. 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)
  7. 用slot和component实现表单共用
  8. Freemarker模板的使用简介
  9. --解决Lock wait timeout exceeded; try restarting transaction
  10. 使用Vim打开十六进制的文件