# 1, How the function input() works

Function input causes the program to pause and wait for the user to enter some text. After getting user input, Python stores it in a variable for easy use.

For example, the following code:

```message=input("Please tell me something.\n")
print(message)
```

The output is as follows:

```Please tell me something.
abc
abc
```

The first abc is your input (can be any value)

Note: when using input(), Python interprets user input as a string. If you want to output a value, you can use int().

Hands on 7-1 – 7-3

```#7-1

car=input("Which car do you want to rent?")
print("Let me see if I can find a "+car+"\n")

#7-2

number=input("How many people?")
if int(number)>8:
print("We don't have enough tables.")
else:
print("Welcome!")
print("\n")

#7-3

if int(num)%10:
print("no")
else:
print("yes")
```

Output:

```Which car do you want to rent?bmw
Let me see if I can find a bmw

How many people?9
We don't have enough tables.

no
```

# 2, while loop

## 1. Use while loop

Run the following code:

```number=1
while number<=5:
print(number)
number+=1
```

The output is as follows:

```1
2
3
4
5
```

## 2.break and continue statements

break statement to exit the entire loop, execute the following statement.
The continue statement exits the loop and returns to the loop determination condition.

Hands on 7-4 – 7-7

```#7-4

pizza=input("Please input a kind of food or input 'quit':")
while pizza!='quit':
pizza=input("Please input a kind of food or input 'quit':")

#7-5

while True:
age = int(input("How old are you? "))
if age < 3:
print("free")
elif age >= 3 and age < 12:
print("10 dollars")
elif age >= 12:
print("15 dollars")

#7-6 7-7

#slightly
```

The output is as follows:

```Please input a kind of food or input 'quit':eggs
Please input a kind of food or input 'quit':meat
Please input a kind of food or input 'quit':quit
How old are you? 2
free
How old are you? 5
10 dollars
How old are you? 15
15 dollars
```

# 3, Use the while loop to work with lists and dictionaries

The for loop is an effective way to traverse the list, but the list should not be modified in the for loop, otherwise it will be difficult for Python to track its elements. To modify the list while traversing it, use the while loop.

## 1. Move elements between lists

Suppose you have a list of newly registered but not yet verified web users; after you verify these users, move them to another list of verified users, as follows:

```unconfirmed_users=['zhansan','lisi','wangwu']
confirmed_users=[]

while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user:"+current_user)
confirmed_users.append(current_user)

print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user)
```

The output is as follows:

```Verifying user:wangwu
Verifying user:lisi
Verifying user:zhansan

The following users have been confirmed:
wangwu
lisi
zhansan
```

## 2. Delete all list elements with specific values

```pets=['cat','dog','goldfish','cat','rabbit']
print(pets)
while 'cat' in pets:
pets.remove('cat')

print(pets)
```

The output is as follows:

```['cat', 'dog', 'goldfish', 'cat', 'rabbit']
['dog', 'goldfish', 'rabbit']
```

## 3. Use user input to fill in the dictionary

```responses = {}
active = True
while active:
response = input("Which mountain would you like to climb someday?")
responses[name] = response

repeat = input("Would you like to let another person resond?(y/n)")
if repeat == 'y':
continue
elif repeat == 'n':
break
print("\n--- Poll Results ---")

for name,response in responses.items():
print(name+" would like to climb "+response+".")
```

The output is as follows:

```What is your name?liming
Which mountain would you like to climb someday?a
Would you like to let another person resond?(y/n)y

Which mountain would you like to climb someday?b
Would you like to let another person resond?(y/n)n

--- Poll Results ---
liming would like to climb a.
zhanghua would like to climb b.

```

Try it 7-8-7-10

```#7-8

sandwich_orders=["a","b","c"]
finished_sandwiches=[]

while sandwich_orders:
sandwich = sandwich_orders.pop()
finished_sandwiches.append(sandwich)

#7-9

sandwich_orders = ["a","pastrami","c","pastrami","pastrami","b"]
print("The pastrami was sold out.")
while "pastrami" in sandwich_orders:
sandwich_orders.remove("pastrami")
print(sandwich_orders)

#7-10

travel_places = {}
while True:
name = input("What is your name?")
travel_places[name] = input("Where do you want to travel?")
judge = input("Do you want to repeat?(y/n)")
if judge == 'y':
continue
elif judge == 'n':
break
for name,place in travel_places.items():
print(name + " want to travel to " +place+".")
```

The output is as follows:

```I made your c sandwich.

The pastrami was sold out.
['a', 'c', 'b']

Where do you want to travel?Pairs
Do you want to repeat?(y/n)y  