message = input("Please tell me your name:") print("Hello , " + message + "!")
+
输出结果如下:
+
1 2
Please tell me your name:anliy Hello , anliy!
+
进阶:
+
1 2 3 4
message = "Please tell me your name so that we can personalize the messages you see." message += "\nWhat's your first name?" name = input(message) print("\nHello , " + name + "!")
+
输出结果如下:
+
1 2 3 4
Please tell me your name so that we can personalize the messages you see. What's your first name?trhx Hello , trhx!
+
- 6.1.1 使用 int() 来获取数值输入
使用函数 input() 时,Python会将用户输入解读为字符串:
+
1 2 3 4
>>> age = input("How old are you?") How old are you?19 >>> age '19'
+
为了解决这个问题,可以使用函数 int() ,它让Python将输入视为数值:
+
1 2 3 4 5
>>> age = input("How old are you?") How old are you?19 >>> age = int(age) >>> age 19
+
实例:
+
1 2 3 4 5 6
age = input("Please tell me your age:") age = int(age) if age >= 18: print("You are old enough to go to the Internet bar!") else: print("You are not old enough to go to Internet bar!")
+
输出结果如下:
+
1 2
Please tell me your age:17 You are not old enough to go to Internet bar!
for 循环用于针对集合中的每一个元素的一个代码块,而 while 循环不断地运行,直到指定的条件不满足为止
+
- 6.2.1 使用 while 循环
一个简单的 while 循环:
+
1 2 3 4
num = 1 while num < 5: print(num) num += 1
+
输出结果如下:
+
1 2 3 4
1 2 3 4
+
- 6.2.2 让用户选择退出循环
1 2 3 4 5 6
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)
+
运行程序:
+
1 2 3 4 5 6 7 8 9 10 11 12
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.Hello everyone! Hello everyone!
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.Hello again! Hello again!
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.quit quit
prompt = "\nTell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program." active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
+
运行结果与6.2.2一致
+
- 6.2.4 使用 break 退出循环
要立即退出 while 循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用 break 语句,break 语句用于控制程序流程,可使用它来控制哪些代码将执行,哪些代码不执行
+
1 2 3 4 5 6 7 8 9
prompt = "\nPlease enter the name of a city you have visited:" prompt += "\nEnter 'quit' when you are finished." active = True while active: city = input(prompt) if city == 'quit': break else: print("I'd love to go to " + city.title() + "!")
+
运行程序:
+
1 2 3 4 5 6 7 8 9 10 11
Please enter the name of a city you have visited: Enter 'quit' when you are finished.Shanghai I'd love to go to Shanghai! Please enter the name of a city you have visited: Enter 'quit' when you are finished.Beijing I'd love to go to Beijing!
Please enter the name of a city you have visited: Enter 'quit' when you are finished.quit
while polling_active: #提示输入被调查者的姓名和回答 name = input("\nWhat's your name?") response = input("What kind of fruit do you like?") #将答卷储存在字典中 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 + " like " + response + ".")
+
运行程序:
+
1 2 3 4 5 6 7 8 9 10 11
What's your name?TRHX What kind of fruit do you like?apple Would you like to let another person respond?(Yes/No)Yes What's your name?TRHXCC What kind of fruit do you like?banana Would you like to let another person respond?(Yes/No)No
------ Poll Results ------ TRHX like apple. TRHXCC like banana.