l  函数input()工作原理

n  示例演示了一种创建多行字符串的方式,第一行将消息的前半部分存储在变量prompt中,第二行,+=在存储在prompt中的字符串末尾附件一个字符串。

例如:

prompt = "If you tell us who you are, we can personalize the messages you see."

prompt +="\nWhat is your first name?\n"

n  在使用时如果输入数字,系统将自动将其转换为字符串。如果想仍作为数字使用,可以将其强制转换为数字,例如整数可以用int()

l  while循环

n  在循环中使用continue,可以继续执行循环,它不会像break语句那样不再执行余下的代码并退出整个循环,而是重新开始这个循环。

n  避免无限循环:如果程序陷入无限循环,可按Ctrl+C,也可以关闭显示程序输出的终端窗口(不适用于Sublime Text

l  使用while循环来处理列表和字典

n  在列表之间移动元素

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

n  使用用户输入来填充字典

# -*- coding: utf-8 -*-

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 respod?(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 + ".")