用户输入和while 循环

函数 input() 的工作原理
"""
函数input() 让程序暂停运行,等待用户输入一些文本。
使用函数input() 时,Python将用户输入解读为字符串。
"""
message= input("Tell me something, and I will repeat it back to you:")#2
print(message)

name = input("Please enter your name: ")
print(f"\nHello, {name}!")

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print(f"\nHello, {name}!")

age = input("How old are you? ")#3
age=int(age)
if age >=18:
    print(age)

'''
Tell me something, and I will repeat it back to you:ad
ad
Please enter your name: yi

Hello, yi!
If you tell us who you are, we can personalize the messages you see.
What is your first name? chen

Hello, chen!
How old are you? 15'''


while 循环简介

"""
要立即退出while 循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break 语句。
要返回循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break 语句那样不再执行余下的代码并退出整个循环。
"""
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
      city = input(prompt)
      if city == 'quit':
          break
      else:
          print(f"I'd love to go to {city.title()}!")

current_number = 0
while current_number < 10:
        current_number += 1
        if current_number % 2 == 0:
             continue
        print(current_number)
'''
Please enter the name of a city you have visited:
(Enter 'quit' when you are finished.) as
I'd love to go to As!

Please enter the name of a city you have visited:
(Enter 'quit' when you are finished.) quit
1
3
5
7
9
'''


"""
定义一个变量,用于判断整个程序是否处于活动状态。这个变量称为标志(flag),充当程序的交通信号灯。可以让程序在标志为True 时继续运行,并在任何事件导致标志的值为False 时让程序停止运行。
"""
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

active = True
while active:
    message = input(prompt)
    if message == 'quit':
         active = False
    else:
         print(message)
'''
1
2
3
4
5

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. 1
1

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
'''


使用 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')
print(pets)

responses = {}
polling_active = True
while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")
    responses[name] = response
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False
print("\n--- Poll Results ---")
for name, response in responses.items():
    print(f"{name} would like to climb {response}.")
'''
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']

What is your name? chen
Which mountain would you like to climb someday? chen
Would you like to let another person respond? (yes/ no) no

--- Poll Results ---
chen would like to climb chen.'''

你可能感兴趣的:(java,前端,服务器)