《Python程式設計》第七章部分課後練習題

Zetrue_Li發表於2018-03-26

#7-4 披薩配料:

程式碼:

#7-4 披薩配料
"""
mushrooms
pepperoni
extra cheese
"""

while True:
	message = 'Which Pizza ingredient would you add?'
	message += "\n(Enter 'quit' to end): "
	ingredient = input(message)
	if ingredient == 'quit':
		break;
	else:
		message = '\nWe will add ' + ingredient + ' in the Pizza. \n'
		print(message)

輸出:pass


#7-5 電影票:

程式碼:

#7-5 電影票

while True:
	message = 'Dear gentleman or lady, How old are you?'
	message += "\n(Enter 'q' to end): "
	year = input(message)
	if year == 'q':
		break;
	else:
		year = int(year)
		if year < 3:
			message = 'You are free admission!'
		elif year <=12:
			message = 'You have to pay 10 dollars for the ticket!'
		elif year > 12:
			message = 'You have to pay 15 dollars for the ticket!'
		print(message+'\n')

輸出:pass


#7-7 無限迴圈:

程式碼:

#7-7 無限迴圈
#可以嘗試,沒什麼大礙的

while True:
	num = list(range(1, 10**4))

輸出:pass


#7-8 熟食店:

程式碼:

#7-8 熟食店

sandwich_orders = ['Reuben sandwich', 'Club sandwich', 'Doner sandwich']
sandwich_orders.append('Hamburger')
finished_sandwiches = list()
while sandwich_orders:
	sandwich = sandwich_orders.pop()
	message = 'I made your ' + sandwich + '.'
	finished_sandwiches.append(sandwich)
	print(message)

while finished_sandwiches:
	print(finished_sandwiches.pop())

輸出:

I made your Hamburger.
I made your Doner sandwich.
I made your Club sandwich.
I made your Reuben sandwich.
Reuben sandwich
Club sandwich
Doner sandwich
Hamburger



#7-9 五香菸薰牛肉(pastrami)賣完了 :

程式碼:

#7-9 五香菸薰牛肉(pastrami)賣完了 

sandwich_orders = ['Reuben sandwich', 'Club sandwich', 'Doner sandwich']
sandwich_orders.append('Hamburger')
for a in range(0,3):
	sandwich_orders.append('pastrami')

message = 'Sorry. We are out of pastrami.\n'
print(message)

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

while sandwich_orders:
	print(sandwich_orders.pop())

輸出:

Sorry. We are out of pastrami.

Hamburger
Doner sandwich
Club sandwich
Reuben sandwich

相關文章