Chapter 7

7-1. Rental Car:

Write a program that asks the user what kind of rental car they would like. Print a message about that car, such as “Let me see if I can find you a Subaru.”

car = input("what kind of car you want?")

print("\n Let me see if I can find you a",car)

7-2. Restaurant Seating:

Write a program that asks the user how many people are in their dinner group. If the answer is more than eight, print a message say- ing they’ll have to wait for a table. Otherwise, report that their table is ready.

person = input("How many persons are in your dinner ?")

person = int(person)

if person > 8:
  print("\n Please wait for table")
else:
  print("Table is ready for you")

7-3. Multiples of Ten:

Ask the user for a number, and then report whether the number is a multiple of 10 or not.

number = input("Please input a number ")
number = int(number)

if number%10==0:
  print("Number is multiple of 10")
else:
  print("number is not multiple of 10")

7-3. Multiples of Ten:

Ask the user for a number, and then report whether the number is a multiple of 10 or not.

name = input("Please write a number")

if int(name)%10==0:
  print(f"number is multiple of 10")
else:
     print(f"Number is not multiple of 10")

7-4. Pizza Toppings:

Write a loop that prompts the user to enter a series of pizza toppings until they enter a ‘quit’ value. As they enter each topping, print a message saying you’ll add that topping to their pizza.

topping = ""
while topping !='quit':
     topping = input("Please enter topping you want? ")
     print(f"We have added the topping")

7-5. Movie Tickets:

A movie theater charges different ticket prices depending on a person’s age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is $10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket.

while True:
    age = input("What is your age? ")
    if age == 'quit':
        break
    age = int(age)
    if age < 3:
        print("Your ticket is free.")
    elif age >= 3 and age <= 12:
        print("Your ticket price is $10.")
    else:
        print("Your ticket price is $15.")

7-6. Three Exits:

Write different versions of either Exercise 7-4 or 7-5 that do each of the following at least once:

  • Use a conditional test in the while statement to stop the loop.

same as above

  • Use an active variable to control how long the loop runs.
active = True

while active:
    age = input("What is your age? ")
    if age == 'quit':
        break
    age = int(age)
    if age < 3:
        print("Your ticket is free.")
    elif age >= 3 and age <= 12:
        print("Your ticket price is $10.")
    else:
        print("Your ticket price is $15.")

Another example

podcast = 'nothing but facts'
channel = ["5 pillars","Hamza yusuf","safina society",'nothing but facts']
no_of_podcasts = 4

while no_of_podcasts<5:
  print("podcast are not enough")
  no_of_podcasts+=1
podcast are not enough

• Use a break statement to exit the loop when the user enters a ‘quit’ value.

liked_podcast = 'safina society'

while type(liked_podcast)!=str:
  country = 'france'
  iftar_place  = 'mosque turkiye'
    break
      else:
    iftar_place = 'mosque omar'
        break

7-7. Infinity:

Write a loop that never ends, and run it. (To end the loop, press CTRL-C or close the window displaying the output.)

age = 10

while age<20:
     print("You are a child")

7-8. Deli:

Make a list called sandwich_orders and fill it with the names of various sandwiches. Then make an empty list called finished_sandwiches. Loop through the list of sandwich orders and print a message for each order, such as I made your tuna sandwich. As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that was made.

sandwich_orders = ['tuna','anday wala burger','sandwich2','sandwich3']

finished_sandwiches = []


while sandwich_orders:
     for sandwich in sandwich_orders:
      finished_sandwiches.append(sandwich)
      print(f"I made you {sandwich} sandwich")
      sandwich_orders.remove(sandwich)
    
print(f"Following sandwiches are made")
I made you tuna sandwich
I made you sandwich2 sandwich
I made you anday wala burger sandwich
I made you sandwich3 sandwich
Following sandwiches are made

7-9. No Pastrami:

Using the list sandwich_orders from Exercise 7-8, make sure the sandwich ‘pastrami’ appears in the list at least three times. Add code near the beginning of your program to print a message saying the deli has run out of pastrami, and then use a while loop to remove all occurrences of ‘pastrami’ from sandwich_orders. Make sure no pastrami sandwiches end up in finished_sandwiches.

sandwich_orders = ['tuna','anday wala burger','sandwich2','sandwich3','pastrami','pastrami','pastrami']

for sandwich in sandwich_orders:
     if sandwich=='pastrami':
          print(f"Deli has runout of {sandwich}")

finished_sandwiches = []

while sandwich_orders:
     for sandwich in sandwich_orders:
       if sandwich == 'pastrami':
           sandwich_orders.remove(sandwich)
       else:
            finished_sandwiches.append(sandwich)
            sandwich_orders.remove(sandwich)

print(finished_sandwiches)
print(sandwich_orders)
Deli has runout of pastrami
Deli has runout of pastrami
Deli has runout of pastrami
['tuna', 'sandwich2', 'anday wala burger', 'sandwich3']
[]

7-10. Dream Vacation:

Write a program that polls users about their dream vaca- tion. Write a prompt similar to If you could visit one place in the world, where would you go? Include a block of code that prints the results of the poll.

poll = True
prompt = f"What is your dream vacation"

while poll:
     location = input("What location u want to visit? ")
     if location == 'quit':
          poll = False
     else:
          print(f"User want to visit {location}")