要加双引号 或 单引号 把输入内容包括
message =input(" Tell me something, and I will repeat it back to you : ")print(message)
结果为
7.1.1 编写清晰的程序
prompt ="If you tell us who you are, we canpersonalize the messages you see."
prompt +="\nWhat is your name?\n "
name =input(prompt)print("Hello, "+ name +"!")
结果为
7.1.2 使用 int() 来获取数值输入
>>> age =input("How old are you?")
How old are you ? 21>>> age =int(age) 从键盘键入的是字符串,需要用int()转为数字
>>> age >=18True
height =input("How tall are you, in inches?")
height =int(height)if height >=36:print("\nYou're tall enough to ride!")else:print("\nYou'll be able to ride when you're a little older.")
prompt ="\nTell me something,and I will repeat it back you : "
prompt +="\nEnter 'quit' to end the program "
message ='' 这里一定要给message一个值,
空值也可以,防止出错,提供下面while
循环 第一次的条件判断
while message !='quit':
message =input(prompt)print(message)if message =='quit':print("\nquit success!")
结果为
7.2.3 使用标志
相当于使用布尔表达式
prompt ="\nTell me something,and I will repeat it back you : "
prompt +="\nEnter 'quit' to end the program "
active =True 布尔表达式
while active :
message =input(prompt)if message =='quit':print("quit success")
active =Falseelse:print(message)
结果为
7.2.4 使用 break 退出循环
prompt ="\nTell me something,and I will repeat it back you : "
prompt +="\nEnter 'quit' to end the program "whileTrue:
city =input(prompt)if city =='quit':print("\nyour city is "+ city)breakelse:print("\nI'd love to go to "+ city.title()+"!")
responses ={}
设置一个标志,指出调查是否继续
polling_active =Truewhile polling_active :
提示输入被调查者的名字和回答
name =input("\n What is your name? ")
response =input("\nWhich mountain would you like to climb someday? ")
将答卷存储在字典中
response[name]= response
看看是否还有人要参与调查
repeat =input("\n 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 +".")
7.1 函数 input() 的工作原理例子要加双引号 或 单引号 把输入内容包括message = input(" Tell me something, and I will repeat it back to you : ")print(message)结果为7.1.1 编写清晰的程序prompt = "If you tell us who you are, we ...