第 7 章 用户输入和while循环
课后题
# 7-1
message = input("What kind car do you need? ")
print("Let me see if I can find you a "+message)
#7-2
message = input("How many people have the dinner? ")
message = int(message)
if message > 8:
print("There is no empty table.")
else:
print("There are some tables.")
#7-3
number = input("Enter a number: ")
number = int(number)
if number%10 == 0:
print(str(number) + " is integer multiple of 10")
else :
print(str(number) + " isn't integer multiple of 10")
# 7-4
prompt = "\nPizza's Burden: "
prompt += "\nEnter 'quit' to end the program: "
message =''
while message != 'quit':
message = input(prompt)
print("We will add "+message)
#7-5
age = input("Age: ")
age = int(age)
if age<3:
print("FREE")
elif age>=3 and age<12 :
print("$10")
else:
print("$15")
#7-6
prompt = "\nPizza's Burden: "
prompt += "\nEnter 'quit' to end the program "
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print("We will add "+message+".")
prompt = "\nPizza's Burden: "
prompt += "\nEnter 'quit' to end the program "
active = True
while active:
message = input(prompt)
if message == 'quit':
break
else:
print("We will add "+message+".")
#7-8
sandwich_orders = ['A','B','C','D']
finished_sandwiches = []
while sandwich_orders:
finished_sandwich = sandwich_orders.pop()
print("I made your " + finished_sandwich.title()+" sandwich")
finished_sandwiches.append(finished_sandwich)
print("I have finished sandwiches: ")
for finished_sandwich in finished_sandwiches:
print('\t'+finished_sandwich)
#7-9
print("Pastrami have sold out.")
sandwich_orders = ['A','B','C','D','pastrami','pastrami','pastrami']
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
#7-10
places = {}
polling_active = True
while polling_active:
name = input('\nWhat is your name? ')
place = input("If you could visit one place in the world,where would you go? ")
places[name]=place
repeat = input("Would you like to let another person respond?(yes/no) ")
if repeat == 'no':
polling_active = False
print('\n=== Poll Results ===')
for name,place in places.items():
print(name + " would visit one place is "+ place +'.')