# 用户输入和 while 循环

## 7.1 函数 `input()` 工作原理

E.g:

``````message = input("Tell me something, and I will repeat it back to you: ")
print(message)
``````
###### Output:
``````Tell me something, and I will repeat it back to you:
Hello, Everyone!
Hello, Everyone!
``````

### 7.1.1 编写清晰的程序

``````name = input("Please enter your name")
print("Hello, " + name + "!")
``````
###### Output:
``````Please enter your name: Eric
Hello, Eric!
``````

### 7.1.2 使用 int() 来获取数值输入

E.g:

``````age = input("How old are you?")
age = int(age)
``````

### 7.1.3 求模运算符 `%`

E.g: even_or_odd.py

``````number = input("Enter a number, and I'll tell you whether it is even or odd: ")
number = int(number)

if number % 2 == 0:
print("\nThe number " + str(number) + " is even.")
else:
print("\nThe number " + str(number) + " is odd.")
``````
###### Output:
``````Enter a number, and I'll tell you whether it is even or odd: 43

The number 43 is odd
``````

## 7.2 `while` 循环简介

### 7.2.1 使用 `while` 循环

E.g: counting.py

``````current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
``````
###### Output:
``````1
2
3
4
5
``````

### 7.2.2 让用户选择何时退出

we can define a variable and evaluate it when entering `while` E.g: parrot.py

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

while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
``````
###### Output:
``````Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program: hello
hello

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
``````

### 7.2.3 使用标志

E.g: improved_parrot.py

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

active = True
while active == True:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
``````

### 7.2.4 Use `break` to quit a loop

E.g: second_inproved_parrot.py

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

while True:
message = input(prompt)
if message == 'quit':
break
else:
print(message)
``````

### 7.2.5 Use `continue`

E.g: counting.py

``````current_number = 0
while current_number < 10:
current_number += 1
if current_number %2 == 0:
continue

print(current_number)
``````
###### Output:
``````1
3
5
7
9
``````

## 7.3 使用 `while` 处理列表和字典

### 7.3.1 在列表中移动元素

E.g: confirmed_users.py

``````unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

while unconfirmed_users:
current_user = unconfirmed_users.pop()

print("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())
``````
###### Output:
``````Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice
``````

### 7.3.2 删除包含特定值的所有列表元素

``````pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)

while 'cat' in pets:
pets.remove('cat')

print(pets)
``````
###### Output
``````['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
``````

### 7.3.3 使用用户输入填充字典

E.g: mountain_poll.py

``````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 Result ---\n")
for name, mountain in responses.items():
print(name.title() + " would like to climb " + mountain.title() + ".")
``````
###### Output:
``````What is your name? bbn
Which mountain would you like to climb someday? a
Would you like to let another person respond? (yes/no)no

--- Poll Result ---

Bbn would like to climb a.
``````

Tags:

Categories:

Updated: