高階程式設計技術作業 8

2021-08-17 15:51:12 字數 3665 閱讀 4590

題目描述:編寫乙個迴圈,提示使用者輸入一系列的比薩配料,並在使用者輸入'quit'時結束迴圈。每當

使用者輸入一種配料後,都列印一條訊息,說我們會在比薩中新增這種配料。

input

potato

tomato

fish

quit

output

please input an ingredient

we will put potato in pizza

please input an ingredient

we will put tomato in pizza

please input an ingredient

we will put fish in pizza

**展示:

while

message != "quit":

message = input("please input an ingredient\n")

ifmessage != 'quit':

print("we will put " + message + " in pizza")

題目描述:有家電影院根據觀眾的年齡收取不同的票價:不到三歲的觀眾免費;三到十二歲的觀眾為

10美元;超過12歲的觀眾為15美元。請編寫乙個迴圈,在其中詢問使用者的年齡,並指出其票價。

input

2 5

15output

how old are you?

2 you don』t need to pay

how old are you?

5 you need to pay 10 dollars

how old are you?

15 you need to pay 15 dollars

**展示:

age = ''

while

1 : age = input("how old are you?\n")

if age == 'quit':

break

num = int(age)

if num < 3

and num > 0:

print("you don't need to pay")

elif num >=3

and num <= 12:

print("you need to pay 10 dollars")

elif num > 12:

print("you need to pay 15 dollars")

題目描述:以另一種方式完成練習7-4或7-5,在程式中愛去如下所有做法。

·在while迴圈中使用條件測試來結束迴圈。

·使用變數active來控制迴圈結束的時機。

·使用break語句在使用者輸入『quit』時退出迴圈。

input

2 5

15output

how old are you?

2 you don』t need to pay

how old are you?

5 you need to pay 10 dollars

how old are you?

15 you need to pay 15 dollars

**展示:

#ver1

age = ''

while age != 'quit' :

age = input("how old are you?\n")

num = int(age)

ifnum< 3

andnum > 0:

print("you don't need to pay")

elif num >=3

andnum

<= 12:

print("you need to pay 10 dollars")

elif num > 12:

print("you need to pay 15 dollars")

#ver2

active = 0

while active != 1:

age = input("how old are you?\n")

if age == 'quit':

active = 1

else:

num = int(age)

ifnum< 3

andnum > 0:

print("you don't need to pay")

elif num >=3

andnum

<= 12:

print("you need to pay 10 dollars")

elif num > 12:

print("you need to pay 15 dollars")

#ver3

while

1 : age = input("how old are you?\n")

if age == 'quit':

break

num = int(age)

ifnum< 3

andnum > 0:

print("you don't need to pay")

elif num >=3

andnum

<= 12:

print("you need to pay 10 dollars")

elif num > 12:

print("you need to pay 15 dollars")

題目描述:建立乙個名為sandwich_orders的列表,在其中包含各種三明治的名字;再建立乙個名為

finished_sandwiches的空列表。遍歷列表sandwich_orders,對於其中的美中三明治,都列印一

條訊息,如i made your tuna sandwich,並將其移到列表finished_sandwiches。所有三明治

都製作好後,列印一條訊息,將這些三明治列出來。

input

output

**展示:

sandwich_orders = ['tuna sandwich', 'cheese sandwich', 'mayo sandwich']

finished_sandwiches =

while sandwich_orders:

sandwich = sandwich_orders.pop()

print('i made your ' + sandwich)

for sandwich in finished_sandwiches:

print(sandwich)

高階程式設計技術作業 5

題目描述 使用乙個for迴圈列印數字1 20 包含 展示 for number in range 1,21 print number input null output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 題目描述 通過給函式rang...

高階程式設計技術作業 7

題目描述 使用乙個字典來儲存一些人喜歡的數字。請想5個人的名字,並將這些名字用作字典中 的鍵 想出每個人喜歡的乙個數字,並將這些數字作為值儲存在字典中。列印每個人的名字和喜歡 的數字。展示 dic for name,number in dic.items print name str number ...

高階程式設計技術hw week8

leetcode 11 container with most water 如下 class solution def maxarea self,height type height list int rtype int maxwater 0 i 0j len height 1 while i解題思...