#练习 7-1:
car = input('what kind of car do you want to rent?\n')
print(f'let me see if i can find you a {car}.')
#练习 7-2:
person = int(input('how many person have a meal?\n'))
if person > 8:
print('there is no empty seat')
else :
print('we have enough seats')
#练习 7-3:
number = int(input('please enter a number\n'))
if number%10 == 0 :
print('This number is an integer multiple of 10.')
else:
print('This number is not an integer multiple of 10.')
#练习 7-4:
pizza ='please enter a series of pizza_ingredients\n'
pizza += "(Enter 'quit' when you are finished.)\n"
while True:
message = input(pizza)
if message == 'quit':
break
else:
print(f'we will add {message} later')
# #练习 7-5:
age = int(input('how old are you? man'))
if age < 3:
print("It's free for you,my boy")
elif age < 12:
print("your ticket price is 10 dollar")
else :
print("your ticket price is 15 dollar")
# #练习 7-6:
#在while循环中使用条件测试来结束循环
inf = 'how old are you? man\n'
inf += "(Enter 'quit' when you are finished.)\n"
age = ''
while age != 'quit':
age = input(inf)
if age != 'quit':
age = int(age)
if age < 3:
print("It's free for you,my boy")
elif age < 12:
print("your ticket price is 10 dollar")
else :
print("your ticket price is 15 dollar")
#使用变量active 来控制循环结束的时机
inf = 'how old are you? man\n'
inf += "(Enter 'quit' when you are finished.)\n"
active = True
while active:
age = input(inf)
if age == 'quit':
active = False
else:
age = int(age)
if age < 3:
print("It's free for you,my boy")
elif age < 12:
print("your ticket price is 10 dollar")
else :
print("your ticket price is 15 dollar")
#使用break语句在用户输入'quit'时退出循环
inf = 'how old are you? man\n'
inf += "(Enter 'quit' when you are finished.)\n"
while True:
age = input(inf)
if age == 'quit':
break
else:
age = int(age)
if age < 3:
print("It's free for you,my boy")
elif age < 12:
print("your ticket price is 10 dollar")
else :
print("your ticket price is 15 dollar")
#练习 7-7:
#Ctrl + c 强制结束
count = 0
n = 0
while count <= 5:
n += count
print(n)
print(n)
#练习 7-8:
sandwish_orders = ['Chacarero','Cemita Cemita','Chip Butty','Barros Luco']
finished_sandwishes = []
while sandwish_orders:
sandwish = sandwish_orders.pop()
print(f'I made your {sandwish} sandwish.')
finished_sandwishes.append(sandwish)
print("Friend_sandwishes are followed:")
for order in finished_sandwishes:
print(f'\n\t{order.title()} sandwish.')
#练习 7-9:
sandwish_orders = ['Chacarero','Cemita Cemita','Chip Butty','Barros Luco','pastrami','pastrami','pastrami']
print('Our pastrami has been sold out')
while 'pastrami' in sandwish_orders:
sandwish_orders.remove('pastrami')
print(sandwish_orders)
#练习 7-10:
place_dream = {}
active = True
names = 'Could you please tell me your name?\n'
inf = 'If you could visit one place in the world,where would you go?\n'
while active:
name = input(names)
place = input(inf)
place_dream[name] = place
repeat = input('Would you like to let another person respond?(yes/no)\n')
if repeat == 'no':
active = False
for name,place in place_dream.items():
print(f"{name.title()}'dream place is {place.title()}")
第7章--用户输入和while循环
最新推荐文章于 2024-07-26 17:36:26 发布