Python Programming: from getting started to practicing exercises after class (Chapter 7 - user input and while loop)

7-1 car rental

Write a program to ask the user what kind of car to rent, and print a message, such as "Let me see if I can find you a Subaru".

message = input("What kind of car would you like to rent? ")
print("Let me see if I can find you a "+ messag)

7-2 restaurant reservation

Write a program to ask how many people eat. If there are more than eight people, print a message indicating that there is no free table; otherwise, indicate that there is a free table.

message = input("How many people will have dinner? ")
num = int(message)
if num > 8:
    print("I'm sorry there's no vacancy.")
else:
    print("We have seats.")

7-3 integral multiple of 10

Ask the user to enter a number and indicate if the number is an integral multiple of 10.

num = input("Please entering a number! ")
num = int(num)
if num % 10 == 0:
    print(str(num) + " is an integral multiple of ten.")
else:
    print(str(num) + " is not an integral multiple of ten.")

7-4 pizza ingredients

Write a loop that prompts the user to enter a series of pizza ingredients and ends the loop when the user enters' quit '. Every time the user enters an ingredient, a message is printed saying that we will add the ingredient to the pizza.

prompt = "\nTell me what do you want to add in your pizza:"
prompt += "\nEnter 'quit' to end the program. "
polling_active = True
while polling_active:
    message = input(prompt)
    if message == "quit":
        polling_active = False
    else:
        print("We will add " + message + " in your pizza.")

7-5 movie tickets

There is a movie theater that charges different fares according to the age of the audience: free for those under 3 years old; 10 dollars for those between 3 and 12 years old; 15 dollars for those over 12 years old. Please write a loop in which you ask the age of the user and indicate their ticket price.

age = input("How old are you? ")
age = int(age)
if age < 3:
    print("Free fare.")
elif 3 < age < 12:
    print("The fare is 10 dollars.")
else:
    print("The fare is 15 dollars.")

7-6 three exits

Complete exercise 7-4 or exercise 7-5 in another way and do all of the following in the procedure.
 use conditional tests in the while loop to end the loop.
 use the variable active to control when the cycle ends.
 use the break statement to exit the loop when the user enters' quit '.

# Rewrite 7-5
active = True
while active:
    age = input("How old are you? ")
    if age == "quit":
        active = False
        break
    age = int(age)
    if age < 3:
        print("Free fare.")
    elif 3 < age < 12:
        print("The fare is 10 dollars.")
    else:
        print("The fare is 15 dollars.")

7-7 infinite cycle

Write an endless loop and run it (to end the loop, press Ctrl +C, or close the window displaying the output).

x = 1
while x <= 5:
    print(x)

7-8 Deli

Create a list called sandwich \ orders that contains the names of various sandwiches, and an empty list called finished \ sandwiches. Traverse the list sandwich ﹣ orders, and for each sandwich, print a message, such as I made your tuna sandwich, and move it to the column finished ﹣ sandwiches. When all sandwiches are made, print a message and list them.

sandwich_orders = ["Italian sandwich","Tunafish sandwich","Sloppy Joe sandwich"]
finished_sandwiches = []
while sandwich_orders:
    sandwich = sandwich_orders.pop()
    print("I made your " + sandwich)
    finished_sandwiches.append(sandwich)
print(finished_sandwiches)

Output results:

I made your Sloppy Joe sandwich
I made your Tunafish sandwich
I made your Italian sandwich
['Sloppy Joe sandwich', 'Tunafish sandwich', 'Italian sandwich']

7-9 pastrami sold out

Use the list sandwich [orders] created to complete exercise 7-8, and make sure 'pastrami' appears at least three times. Add the following code near the beginning of the program: print a message to indicate that the spiced smoked beef in the Deli is sold out; then use a while loop to delete 'pastrami' in the list sandwich [orders]. Verify that 'pastrami' is not included in the final list.

print("Pastrami is sold out!")
sandwich_orders = ["Italian sandwich","pastrami","Tunafish sandwich","pastrami","pastrami","Sloppy Joe sandwich"]
finished_sandwiches = []
while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami")
while sandwich_orders:
    sandwich = sandwich_orders.pop()
    print("I made your " + sandwich)
    finished_sandwiches.append(sandwich)
print(finished_sandwiches)

Output results:

Pastrami is sold out!
I made your Sloppy Joe sandwich
I made your Tunafish sandwich
I made your Italian sandwich
['Sloppy Joe sandwich', 'Tunafish sandwich', 'Italian sandwich']

7-10 dream Resort

Write a program to investigate the user's dream resort. Use tips like "If you could visit one place in the world, where would you go?" and write a block of code to print the survey results.

responses = {}
polling_active = True
while polling_active:
    name = input("\nWhat is your name? ")
    respons = input("If you could visit one place in the world , where would you go ? ")
    responses[name] = respons
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False
print("\n--- Poll Results ---")
for name,respons in responses.items():
    print(name + "would want to go "+ respons + ".")
Published 5 original articles, praised 0, visited 31
Private letter follow

Posted on Wed, 15 Jan 2020 01:56:29 -0800 by JamieinNH