《Python编程从入门到实践》第七章 用户输入和while循环

1. 知识点汇总

2. 练习题

# 练习7.1 汽车租赁
prompt=input("Let me see if i can find you a Subaru\n")
print(prompt)


# 练习7.2 餐馆定位
print('please tell me how many people do you have')
number = int(input())
if number <= 8:
    print("The reservation is successful")
else:
    print("The reservation is not successful")


# 练习7.3 10的整数倍
number = int(input())
if number%10==0:
    print("a multiple of ten")
else:
    print("not a multiple of ten")


# 练习7.4 比萨配料
print("please input the toppings you want")
while True:
    topping=input()
    if topping!='quit':
        print(f"add {topping}")
    else:
        break


# 练习7.5 电影票
print("pleasse tell me your age")
age=int(input())
if age<3:
    print("free")
elif 3<=age<12:
    print("10 dollars")
else:
    print("15 dollars")


# 练习7.6 三种出路
print("please input the toppings you want")
active=True
while active:
    topping=input()
    if topping!='quit':
        print(f"add {topping}")
    else:
        active=False
# 练习7.7
#ctrl+c 结束无限循环


# 使用while处理列表和字典
# 验证用户
unconfirmed_users=['aa','bb','cc']
confirmed_users=[]
while unconfirmed_users:
    current_users=unconfirmed_users.pop()
    print(f"veryfing user:{current_users}")
    confirmed_users.append(current_users)
# 显示所有的已验证用户
print("\n the following users have been confirmed: ")
for user in confirmed_users:
    print(user)


pets=['cat','dog','cat','duck','pig','cat']
while 'cat' in pets:
    pets.remove('cat')
print(pets)



# 练习7.8 熟食店
sandwich_orders=['meat','egg','cheese','bacon']
finished_sandwich=[]
while sandwich_orders:
    mid_sandwich=sandwich_orders.pop()
    print(f"i made your {mid_sandwich} sandwich")
    finished_sandwich.append(mid_sandwich)

print(finished_sandwich)

# 练习7.9 五香烟熏牛肉买完了
sandwich_orders=['meat','pastrami','egg','pastrami','cheese','pastrami','bacon']
print("the pastrami sandwich sell out")
while 'pastrami'  in sandwich_orders:
    sandwich_orders.remove('pastrami')
print(sandwich_orders)


# 练习 7.10 梦想中的度假胜地
print('if you could visit one place in the world,where would you go')
place=[]
active=True
while active:
    placee=input()
    if placee !='quit':
        place.append(placee)
    else:
        active=False
print(place)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值