Python Programming: from introduction to practice -- Chapter 7: user input and while loop

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

num=input("Please input a number:")
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.


Please input a number:12
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':
    print("We will add "+pizza +" in your pizza")
    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
We will add eggs in your pizza
Please input a kind of food or input 'quit':meat
We will add meat in your pizza
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:
    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 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

What is your name?zhanghua
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)
    print("I made your "+sandwich+" 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.
I made your b sandwich.
I made your a sandwich.

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

What is your name?Liming
Where do you want to travel?Pairs
Do you want to repeat?(y/n)y
What is your name?Zhangsan
Where do you want to travel?Beijing
Do you want to repeat?(y/n)n
Liming want to travel to Pairs.
Zhangsan want to travel to Beijing.
Published 6 original articles, won praise 0, visited 338
Private letter follow

Tags: Python

Posted on Sat, 01 Feb 2020 00:19:18 -0800 by bigfunkychief