= input("what kind of car you want?")
car
print("\n Let me see if I can find you a",car)
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.”
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.
= input("How many persons are in your dinner ?")
person
= int(person)
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.
= input("Please input a number ")
number = int(number)
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.
= input("Please write a number")
name
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':
= input("Please enter topping you want? ")
topping 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:
= input("What is your age? ")
age if age == 'quit':
break
= int(age)
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.
= True
active
while active:
= input("What is your age? ")
age if age == 'quit':
break
= int(age)
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
= 'nothing but facts'
podcast = ["5 pillars","Hamza yusuf","safina society",'nothing but facts']
channel = 4
no_of_podcasts
while no_of_podcasts<5:
print("podcast are not enough")
+=1 no_of_podcasts
podcast are not enough
• Use a break statement to exit the loop when the user enters a ‘quit’ value.
= 'safina society'
liked_podcast
while type(liked_podcast)!=str:
= 'france'
country = 'mosque turkiye'
iftar_place break
else:
= 'mosque omar'
iftar_place 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.)
= 10
age
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.
= ['tuna','anday wala burger','sandwich2','sandwich3']
sandwich_orders
= []
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.
= ['tuna','anday wala burger','sandwich2','sandwich3','pastrami','pastrami','pastrami']
sandwich_orders
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.
= True
poll = f"What is your dream vacation"
prompt
while poll:
= input("What location u want to visit? ")
location if location == 'quit':
= False
poll else:
print(f"User want to visit {location}")