python编程从入门到实践第七章习题

第七章介绍的是python的输入函数input(),和while循环,并说明了python的循环可以使用continue和break来实现循环的 一些要求。并且在本章还说明了字符转化为int类型数字的方法。




#7-1
car = input("Let me see if I can find you a Subaru")
print("Oh, you want to find a "+ car)

#7-2
seats = input("How many people are eating here?")
int(seats)
if seats > 8:
	print("Here is no spare table for me")
else print("Here is some spare tables")

#7-3
number = input("Please input a number")
int(number)
if number % 10 == 0:
	print("It is a multiplier of 10.")
else print("It is not a multiple of ten.")

#7-4
while True:
	ingredient = input("Plese input what you want,if you want to stop you can input quit:")
	if ingredient == 'quit':
		break
	else print("We will add " + ingredient + " to your pizza")

#7-8
sandwich_orders = ['First','Second','Third']
finshed_sandwich =[]
# for sandwich in sandwich_orders:
# 	finshed_sandwich.append(sandwich)
# 	sandwich_orders.remove(sandwich)
# 	print("I made the " + sandwich + " sandwich.")

#The above way is a wrong answer for the for statement will find the second message, 
#which is 'Third', when the 'First' is deleted. But as expected, it should be 'Second',
#so the for statement will search the list as [0],[1]..., but as the first element, the second element.
#So when you would deleted some element of a list, you should use for statement carefully.

while sandwich_orders:
	sandwich = sandwich_orders[0]
	finshed_sandwich.append(sandwich)
	sandwich_orders.remove(sandwich)
	print("I made the " + sandwich + " sandwich.")

#or another way to solve this problem
while  sandwich_orders:
	sandwich = sandwich_orders.pop()
	finshed_sandwich.append(sandwich)
	print("I made the " + sandwich + " sandwich.")

#7-9
sandwich_orders = ['pastrani','Bst','pastrani','pastrani']
print("熟食店的五香熏肉已经卖完了")
while 'pastrani' in sandwich_orders:
	sandwich_orders.remove('pastrani')
print(sandwich_orders)

因为sublime并不支持输入,所以这里在dos下编译了。总的来说,这张还是比较简单,只有第八小题有些难度,但是只要小心点还是没问题的,具体解法可以看下上面代码和注释。
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值