码迷,mamicode.com
首页 > 其他好文 > 详细

第7章 用户输入和while循环

时间:2020-02-01 23:42:34      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:input   let   while   结束   person   方便   ack   tin   sage   

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

message = input("Tell me something, and I will repeat it back to you: ")
print(message)

输出
Tell me something, and I will repeat it back to you: Hello everyone!
Hello everyone!

height = input("How tall are you, in inches? ")
height = int(height)#int()用于将获取的输入转换为整型数格式
if height >= 36:
print("\nYou‘re tall enough to ride! ")
else:
print("\nYou‘ll be able to ride when you‘re a little older. ")

while循环

number = 1
while number <= 5:
print(number)
number += 1

遇到break,立即跳出循环
遇到continue,结束本次循环,进入下一次循环

使用用户输入来填充字典
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 Result ---")
for name, response in responses.items():
print(name.title() + "would like to climb" + response + ".")

第7章 用户输入和while循环

标签:input   let   while   结束   person   方便   ack   tin   sage   

原文地址:https://www.cnblogs.com/dingdangsunny/p/12250319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!