关键记忆点:

input()函数、int()、while current_number<=5:、使用标志、break、continue、remove()、while循环与列表和字典


一、input()函数的工作原理

message=input("tell me something,i will repeat it back to you\n ")
#input()括号内容为提示,当用户输入什么,按下enter,屏幕就会输出什么
print(message)

Python第六课——用户输入和while循环_int()

1、编写清晰的提示

prompt=\
    "if you share your name,we can personalize the messages you see."
prompt+="\nwhat's your first name?"
print(f"{prompt}\n")

name=input()
print(f"\nhello,{name}")

Python第六课——用户输入和while循环_break_02

2、使用int()来获取数值输入

age=input("how old are you?\n")
print(age)
age=input("how old are you?\n")
print(int(age))#将字符转成数字
age=input("how old are you?\n")
print(age)
age=input("how old are you?\n")
print(int(age))#int()将字符串转换为数字

二、while循环

1、使用while循环

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

Python第六课——用户输入和while循环_int()_03

由循环条件判断继续还是结束

2、让用户选择何时退出

prompt="please input something\n"
prompt+="enter 'quit' will exit the input\n"
message=""
while message!="quit":
    message=input(prompt)
    if message!="quit":
        print(message)

Python第六课——用户输入和while循环_while循环_04

3、使用标志

prompt="please input something\n"
prompt+="enter 'quit' will exit the input\n"
active=True
while active:
    message=input(prompt)
    if message=="quit":
        active=False#设置标志
    else:
        print(message)

Python第六课——用户输入和while循环_int()_05

4、使用break语句退出循环

prompt='please input the name of your friends'
prompt+='\nenter"quit"when you finished\n'
while True:
    name=input(prompt)
    if name=='quit':
        break#终止后面所有的循环
    else:
        print(f"my fiend is{name}")

Python第六课——用户输入和while循环_int()_06

5、在循环中使用continue

prompt='please input the name of your friends'
prompt+='\nenter"quit"when you finished\n'
while True:
    name=input(prompt)
    if name=='quit':
        continue#只是跳过本次循环
    else:
        print(f"my fiend is{name}")

Python第六课——用户输入和while循环_break_07

6、避免无限循环

按下ctr+c、关闭显示程序输出的终端窗口

三、使用while循环处理列表和字典

1、在列表之间移动元素

#首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users=['alice','brain','candace']
confirmed_users=[]
#验证每个用户,知道验证完所有未验证用户
#将每个已验证用户移到已验证用户列表中
while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    #pop是Python中用于从数据结构中移除元素的函数

    print(f"verify users:{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())

Python第六课——用户输入和while循环_while循环_08

2、删除为特定值的所有列表元素

pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
    pets.remove("cat")#循环移除
print(pets)

Python第六课——用户输入和while循环_continue_09

3、使用用户输入填充字典

responses={}
#设置一个标志,指出调查是否继续
polling_active=True

while polling_active:
#提示输入别调查者的名字和回答
    name=input("\nwhat is your name?")
    response=input("what is your favorite food?")
#将回答存入字典
    responses[name]=response
#看看是否还有人要参与调查
    repeat=input(("would you like to another respond?(yes or no)"))
    if repeat == 'no':
        polling_active=False
#调查结束,显示结果
print("\n___poll results___")
for name,response in responses.items():
    print(f"{name} would lilke to eat  {response}")

Python第六课——用户输入和while循环_input()_10