input
函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便使用。
函数input() 接受一个参数:即要向用户显示的提示 或说明,让用户知道该如何做
我在下面代码中需要输入的地方输入10
strin = input("This is 输入字符串的 \n") #输入字符串的
strin = int(strin) #将字符串形式转换为数值表示
if strin >= 36: #进行数值比较
print("This is a 中年人\n")
else:
print("This is a 年轻人")
if strin % 2 == 0: # %是求模运算符,它将两个数相除并返回余数
print(str(strin) + " is 偶数") #str(strin) 是将变量strin从数值转换为字符串表示
else:
print(str(strin) + " is 奇数")
输出:
This is 输入字符串的
10
This is a 年轻人
10 is 偶数
练习:10的整数倍: 让用户输入一个数字,并指出这个数字是否是10的整数倍
number = input("请输入一个数字: ")
number = int(number)
if number % 10 == 0:
print(str(number) + "能被10整除")
else:
print(str(number) + "不能被10整除")
while循环
for 循环用于针对集合中的每个元素都一个代码块,而while 循环不断地运行,直到指定的条件不满足为止。
让用户选择何时退出
可使用while 循环让程序在用户愿意时不断地运行,我们在其中定义了一个退出值,只要用户输入的不是这个值,程序就接着运行
prompt = "你可以选择输入一条信息"
prompt += "或者输入'退出'进行退出\n"
message = ""
while message != "退出": #当用户输入特定值时才结束循环
message = input(prompt)
if message != "退出":
print(message)
标志,break continue
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志 ,充当了程序的交通信号灯。这样,在while 语句中就只需检查一个条件——标志的当前值是否为True ,并将所有测试(是否发生了应将标志设置为False 的事件)都放在其他地方,从而让程序变得更为整洁。
prompt = "你可以选择输入一条信息"
prompt += "或者输入'退出'进行退出\n"
active = True #添加一个标志
while active: #只检查active的值是否为True
message = input(prompt)
if message == "退出": #当用户输入"退出"时 标志 active 的值变为 False
active = False
else:
print(message)
如果需要马上退出while循环,不再运行循环中剩下的代码,也不管条件测试的结果怎么样,可使用break.
prompt = "请输入你到过的城市(输入退出以表示输入完毕):"
active = True
while active:
city = input(prompt)
if city == "退出":
break #跳出循环
# active = False
else:
print(city)
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue 语句
number = 0
while number < 10 :
number += 1
if number % 2 == 0:
continue #重新跳到while进行执行 也就是重新判断了一遍number < 10 ,执行了一遍了 number = number + 1.
print(number)
输出:
1
3
5
7
9
练习:
披萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。
每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料
电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价
在列表之间移动列表
假设有一个列表,其中包含新注册但还未验证的网站用户;验证这些用户后,如何将他们移到另一个已验证用户列表中呢
users = ["bob","angel","john"] #创建一个待验证的列表
register = [] #穿件一个已验证的空列表
while users :
user = users.pop() #在循环中穿件一个变量存储 users 中已经验证的用户
print(user.title())
register.append(user) #将已验证的用户添加到 register 中
print(register)
输出:
John
Angel
Bob
['john', 'angel', 'bob']
删除包含特定值的所有列表元素
假设你有一个宠物列表,其中包含多个值为'cat' 的元素。要删除所有这些元素,可不断运行一个while 循环,直到列表中不再包含值'cat'
pets = ["cat","dog","cat","rabbit","fish","cat","pig"]
while "cat" in pets:
pets.remove("cat")
print(pets)
输出:
['dog', 'rabbit', 'fish', 'pig']
使用用户输入来填充字典
可使用while循环提示用户输入任意数量的信息。下面来创建一个调查程序,其中的循环每次执行时都提示输入被 调查者的名字和回答
dict = {} #创建字典
active = True #设置一个标志,指出调查是否继续
while active :
name = input("请输入你的名字:\n")
question = input("请输入你的问题:\n")
dict[name] = question #将答卷存储到字典中
print("是否还继续参加调查:\n")
repeat = input("")
if repeat == "no":
active = False
print(dict)
练习:
熟食店:创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。
遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,
sandwich_orders = ["radhot","humburger","asuasge","cola"]
finished_sasndwiches = []
while sandwich_orders :
sandwich_order = sandwich_orders.pop()
print(" I made you " + sandwich_order + "sandwhch")
finished_sasndwiches.append(sandwich_order)
print(finished_sasndwiches)
输出:
I made you colasandwhch
I made you asuasgesandwhch
I made you humburgersandwhch
I made you radhotsandwhch
['cola', 'asuasge', 'humburger', 'radhot']
梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
dream = {}
print("If you could visit one place in the world, where would you go?")
active = True
while active :
name = input("请输入你的姓名:\n")
question = input("你想去哪度假?\n")
dream[name] = question
reqeat = input("还继续调查吗?")
if reqeat == "no":
active = False
for name,question in dream.items():
print("调查人: " + name +" 最想去的地方是 " + question)