第7章--用户输入和while循环

#练习 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()}")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-阿呆-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值