5.1 函数input()工作原理
# 变量=input(参数:说明/提示)
message input("Tell me something,and I will repeat it back to you:"
print(message)
prompt ="If you tell us who you are,we can personalize the messages you see."
# prompt=prompt+"\nWhat is your first name.?"的简写(好用)
prompt += "\nWhat is your first name?
name = input(prompt)
print(f"\nHello,{name}!")
5.2 while
current number = 1
while current_number <=5:
print(current_number)
# current number current number + 1
current_number += 1
5.3 使用while循环处理列表和字典
#首先,创健一个末验证用户列婊
#和一个用于存储已验证用户的空列表
unconfirmed_users ['alice','brian','candace']
confirmed_users =[
#验证每个用户,直到没有末验证用户为止。
#将每个经定过验证的用户都移到已验证用户列表中
while unconfirmed_users:
current_user unconfirmed_users.pop()
print(f"Verifying user:(current_user.title())")
confirmed_users.append(current_user)
#显示所有已验证的用户。
print("nThe following users have been confirmed:"
for confirmed_user in confirmed_users:
print(confirmed_user.title())
# 删除为特定值的所有的列表元素
pets ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat'in pets:
pets.remove('cat')
# 使用用户输入来填充字典
#创健1个空字典
responses ={}
#设置一个标志,指出调查是否继续。只要为True,pythona就运行whie循环中的/代码
polling_active True
while polling_active:
name input("\nWhat is your name?"
mountain_name input("Which mountain would you like to climb someday?")
responses[name]mountain_name
repeat input("Would you like to let another person respond?(yes/no)")
if repeat =='no':
polling_active False
#调查结束,显示结果
print("\n--Poll Results--")
for name,mountain_name in responses.items():# 字典属于不可迭代对象,需要加items才能在for中循环
print(f"{name}would like to climb {mountain_name}.")
标签:confirmed,name,用户,while,print,input,输入,users From: https://www.cnblogs.com/pgl6/p/18327034