《高级编程技术》作业[7]——第七章部分习题选做

本文通过几个实际案例展示了Python编程的应用,包括用户输入处理、条件判断、循环操作以及列表管理等核心概念。通过这些例子,读者可以了解到如何使用Python解决常见问题,如验证数字是否为10的倍数、根据不同年龄计算票价、制作三明治订单并完成订单等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#7-1
car = input("What brand of car do you want?  -")
print("Let me see if I can find you a " + car + '\n')

#7-3
num = input("Please input a number: ")
if int(num) % 10 == 0:
    print("It's is an integral multiple of 10\n")
else:
    print("It's not an integral multiple of 10\n")

#7-5
while True:
    age = input("What is your age please? (enter 'exit' to quit) \n")
    if age == 'exit':
        break
    age_num = int(age)
    price = 0
    if age_num < 3:
        price = 0
    elif age_num <= 12:
        price = 10
    else:
        price = 15

    if price == 0:
        print("It free, baby!")
    else:
        print("It " + str(price) + " dolars, my friend")
print('\n')

#7-8
sandwich_orders = ['good', 'black', 'white', 'tuna', 'black']
finished_sandwiches = []
while len(sandwich_orders) > 0:
    sandwich = sandwich_orders.pop()
    print("I made your " + sandwich + " sandwich")
    finished_sandwiches.append(sandwich)
print("All the sandwiches I made are: ")
for sandwich in finished_sandwiches:
    print("\t" + sandwich)
print('\n')

#7-9
sandwich_orders = ['good', 'pastrami', 'white', 'pastrami', 'tuna', 'pastrami', 'black']
print(sandwich_orders)
finished_sandwiches = []
print("There is no pastrami")

while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami")

while len(sandwich_orders) > 0:
    sandwich = sandwich_orders.pop()
    print("I made your " + sandwich + " sandwich")
    finished_sandwiches.append(sandwich)
print("All the sandwiches I made are: ")
for sandwich in finished_sandwiches:
    print("\t" + sandwich)

输入:

BMW

2

6
99
3
2
exit

输出:

What brand of car do you want?  -BMW
Let me see if I can find you a BMW

Please input a number: 2
It's not an integral multiple of 10

What is your age please? (enter 'exit' to quit)
6
It 10 dolars, my friend
What is your age please? (enter 'exit' to quit)
99
It 15 dolars, my friend
What is your age please? (enter 'exit' to quit)
3
It 10 dolars, my friend
What is your age please? (enter 'exit' to quit)
2
It free, baby!
What is your age please? (enter 'exit' to quit)
exit

I made your black sandwich
I made your tuna sandwich
I made your white sandwich
I made your black sandwich
I made your good sandwich
All the sandwiches I made are:
        black
        tuna
        white
        black
        good


['good', 'pastrami', 'white', 'pastrami', 'tuna',
 'pastrami', 'black']
There is no pastrami
I made your black sandwich
I made your tuna sandwich
I made your white sandwich
I made your good sandwich
All the sandwiches I made are:
        black
        tuna
        white
        good

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值