《Python编程-从入门到实践》第7、8章习题选练

本文通过一系列Python编程练习,涵盖了输入输出、列表操作、循环控制、函数使用等核心概念。包括判断数是否为10的倍数、模拟制作三明治的过程、管理三明治订单、创建描述城市功能、增加专辑信息字段以及处理魔术师列表的副本。这些练习旨在提升对Python编程的理解和应用能力。

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

7-1. Rental Car: Write a program that asks the user what kind of rental car they would like. Print a message about that car, such as “Let me see if I can find you a Subaru.”

知识点分析:Python输入函数input的简单应用

代码:

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

7-3. Multiples of Ten: Ask the user for a number, and then report whether the number is a multiple of 10 or not.

知识点分析:输入练习、%运算符

代码:

#7-3
num = input('please enter an integer\n')
divisible = True if int(num) % 10 == 0 else False

if divisible:
    print(num+" is a multiple of 10")
else:
    print(num+" is NOT a multiple of 10")
7-5. Movie Tickets: A movie theater charges different ticket prices depending on a person’s age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is $10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket.

7-6. Three Exits: Write different versions of either Exercise 7-4 or Exercise 7-5 that do each of the following at least once:
• Use a conditional test in the while statement to stop the loop.
• Use an active variable to control how long the loop runs.

• Use a break statement to exit the loop when the user enters a 'quit' value.

知识点分析:用户输入交互与while循环

代码:

#7-5&7-6

#solution 1
age = input("Please enter your age('quit' to exit)\n")
while age != 'quit':
    if int(age) < 3:
        print("free")
    elif 3 <= int(age
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值