1.用户输入
#输入
message = input("Tell me something,and I will repeat it back to you:")
print(message)
#数值输入
while True:
age = int(input("Enter ' 0 when you are finished.How old are you?"))
if age >= 18:
print("已成年!")
elif age < 18 and age >0:
print("未成年!")
continue
elif age == 0:
break
Tell me something,and I will repeat it back to you:Hi!
Hi!
Enter ' 0 when you are finished.How old are you?20
已成年!
Enter ' 0 when you are finished.How old are you?12
未成年!
Enter ' 0 when you are finished.How old are you?0
while循环
#while条件循环
current_number = 1
numbers = []
while current_number <= 5:
numbers.append(current_number)
#跳出循环的条件
current_number += 1
print(numbers)
print(current_number)
[1, 2, 3, 4, 5]
6
2.创建多行字符串、标志、break、continue
#创建多行字符串,即字符串相加
prompt = "If you tell us who you are,we can personalize the messages"
prompt += "nEnter ''quit' when you are finished.What is your first name?"
print("n第一:")
name = input(prompt)
print(f"n第一个输出:Hello,{name}!n")
#设置初始运行值
print("n第二:")
name = ""
while name != 'quit':
name = input(prompt)
#print(name)
#仅在不是quit值时打印
if name != 'quit':
print(f"n第二个输出:{name}!n")
#设置标志flag
print("n第三:")
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(f"n第三个输出:{message}。n")
#break跳出整个循环
print("n第四:")
while True:
city = input(prompt)
if city == 'quit':
break
else:
print(f"n第四个输出:I'd love to go to {city.title()}n")
#continue跳出一次循环后返回循环开头
print("n第五:")
current_number = 0
while current_number < 10:
current_number += 1
if current_number %2 == 0:
continue
print(current_number)
第一:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?li第一个输出:Hello,li!
第二:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?ni第二个输出:ni!
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?hao第二个输出:hao!
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?quit
第三:
If you tell us who you are,we can personalize the messages
PS C:Userstw> & python d:/MyProgramFiles/VScode/ChapterTow.py第一:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?li第一个输出:Hello,li!
第二:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?ni第二个输出:ni!
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?hao第二个输出:hao!
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?quit第三:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?ta第三个输出:ta。
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?hao第三个输出:hao。
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?quit第四:
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?dou第四个输出:I'd love to go to Dou
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?hao第四个输出:I'd love to go to Hao
If you tell us who you are,we can personalize the messages
Enter ''quit' when you are finished.What is your first name?quit第五:
1
3
5
7
9
3.列表间移动元素、删除重复出现的特定值
#在列表之间移动元素
#首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表。
un/confirm/ied_users = ['alien','brian','candnce']
/confirm/ied_users = []
#验证每个用户,直到没有未验证用户为止。
#将每个经过验证的用户都移动到已验证用户列表中。
#列表为空即为False。
while un/confirm/ied_users:
current_user = un/confirm/ied_users.pop()
print(f"Verifying user: {current_user.title()}")
/confirm/ied_users.append(current_user)
#显示所有已验证的用户。
print("nThe following users have been /confirm/ied:")
for /confirm/ied_user in /confirm/ied_users:
print(/confirm/ied_user.title())
#删除重复出现的特定值
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
while 'cat' in pets:
pets.remove('cat')
print(pets)
Verifying user: Candnce
Verifying user: Brian
Verifying user: Alien
The following users have been /confirm/ied:
Candnce
Brian
Alien
['dog', 'dog', 'goldfish', 'rabbit']
4.用户输入填充字典
#使用用户输入来填充字典
responses ={}
#设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
#提示输入被调查者的名字和回答。
name =input("nWhat is your name?")
response = input("nWhich mountain would you like to climb someday?")
#将回答存储在字典中。
responses[name] = response
#看看是否还有人要参与调查。
respeat = input("nqWould you like to let another person respond?(y/n)")
if respeat == 'n':
polling_active = False
#调查结束,显示结果。
print("n--- Poll Results ---")
print(responses)
for name,response in responses.items():
print(f"{name} would like to climb {response}")
What is your name?Li
Which mountain would you like to climb someday?huangshan
qWould you like to let another person respond?(y/n)y
What is your name?cc
Which mountain would you like to climb someday?baishan
qWould you like to let another person respond?(y/n)n
--- Poll Results ---
{'Li': 'huangshan', 'cc': 'baishan'}
Li would like to climb huangshan
cc would like to climb baishan



