python作业ch7和ch8

本文通过几个简单的Python编程实例,包括判断整数倍数、票价计算、三明治订单处理及显示等,展示了Python语言的基本用法和流程控制。此外,还介绍了如何使用函数来组织代码并传递参数。

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

7-3

num = int(input('please input a number:'))
if num % 10 == 0:
print('This number is an integer multiple of 10.')
else:
print('This number is not an integer multiple of 10.')


7-5

age = input('Please input your age:(input "q" to quit)')
while age != 'q':
age = int(age)
if age < 3:
print('You are free of chareg.')
elif age < 12:
print('The price of your ticket is 10 dollars.')
else:
print('The price of your ticket is 15 dollars.')
age = input('Please input your age:(input "q" to quit)')

7-8

sandwich_orders = ['pastrami', 'ham', 'chicken', 'pastrami', 'cheese', 'pastrami']
finished_sandwiches = []
while sandwich_orders:
print('I made your tuna sandwich.')
finished_sandwiches.append(sandwich_orders.pop())
print(sandwich_orders)
print(finished_sandwiches)
output:

7-9:

sandwich_orders = ['pastrami', 'ham', 'chicken', 'pastrami', 'cheese', 'pastrami']
finished_sandwiches = []
print('pastrami was sold out')
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
while sandwich_orders:
print('I made your tuna sandwich.')
finished_sandwiches.append(sandwich_orders.pop())
print(finished_sandwiches)

output:



8-2

def favorite_book(title):
print('One of my favorite book is ' + title + '.')

favorite_book('Alice in Wonderland')

output:



8-5

def describec_city(city, country = 'China'):
print(city + ' is in ' + country + '.')
describec_city('Beijing')
describec_city(city = 'Guangzhou')
describec_city('London', country = 'England')

output:


8-12

def sandwich(*toppings):
print('The types of sandwich include:')
for topping in toppings:
print(topping)

sandwich('pastrami', 'ham', 'chicken')

output:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值