栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 用户输入、while循环、break、continue、标志

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 用户输入、while循环、break、continue、标志

 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号