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

时间:2022-08-04 00:37:02

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

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

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

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

#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下编译了。总的来说,这张还是比较简单,只有第八小题有些难度,但是只要小心点还是没问题的,具体解法可以看下上面代码和注释。