《Python程式設計從入門到實踐》第七章414

2021-08-17 15:55:29 字數 2638 閱讀 3577

編寫乙個程式,詢問使用者要租賃什麼樣的汽車,並列印一條訊息,如』let me see if i can find you a subaru』。

car = input("waht kind of car would you like to rent?")

print("let me see if i can find you a " + car)

編寫乙個程式,詢問使用者有多少人用餐。如果超過8人,就列印一條訊息,指出沒有空桌;否則指出有空桌。

num = int(input("how many seats would you like?"))

if num > 8:

print("full")

else:

print('acceptable')

讓使用者輸入乙個數字,並指出這個數字是否是10的整數倍。

num = int(input('input a integer'))

if num % 10 == 0:

print("10s")

else:

print("not 10s")

編寫乙個迴圈,提示使用者輸入一系列的比薩配料,並在使用者輸入』quit『時結束迴圈。每當使用者輸入一種配料後,都列印一條訊息,說我們會在比薩中新增這種配料。

food = input('input some addtitions')

while food != 'quit':

print('we will add ' + food)

food = input()

有家電影院根據觀眾的年齡收取不同的票價:不到3歲的觀眾免費;3~12歲的觀眾為10美元;超過12歲的觀眾為15美元。請編寫乙個迴圈,在其中詢問使用者的年齡,並指出其票價

food = input('input some addtitions')

while food != 'quit':

print('we will add ' + food)

food = input()

以另一種方式完成練習7-4或練習7-5,在程式中採取如下所有做法。 略

編寫乙個沒完沒了的迴圈,並執行它(要結束該迴圈,可按ctrl+c,也可關閉顯示輸出的視窗)。

while

true:

pass

建立乙個名為sanwich_orders的列表,在其中包含各種三明治的名字;在建立乙個名為finished_sanwiches的空列表。遍歷列表sanwich_orders,對於其中的每種三明治,都列印一條訊息,如i made your tuna sanwich,並將其移到列表finished_sanwiches。所有三明治都製作好後,列印一條訊息,將這些三明治列出來。

sanwich_orders = ['a sanwich', 'b sanwich', 'c sanwich']

finished_sanwiches =

while sanwich_orders:

sanwich = sanwich_orders.pop()

print('i made your ' + sanwich)

print("all are done")

print(','.join(finished_sanwiches))

使用為完成練習7-8而建立的列表sanwich_orders,並確保』pastrami』在其中至少出現了三次。在程式開頭附近新增這樣的**:列印一條訊息,指出熟食店的五香菸薰牛肉賣完了;再使用乙個while迴圈將列表sanwich_orders中的』pastrami』都刪除。確認最終的列表finished_sanwiches中不包含』pastrami』。

sanwich_orders = ['a sanwich', 'pastrami', 'b sanwich', 'c sanwich', 'pastrami']

finished_sanwiches =

print("pastrami has been sold out")

while

'pastrami'

in sanwich_orders:

sanwich_orders.remove('pastrami')

while sanwich_orders:

sanwich = sanwich_orders.pop()

print('i made your ' + sanwich)

print("all are done")

print(','.join(finished_sanwiches))

編寫乙個程式,調查使用者夢想的度假勝地。使用類似於』if you could visit one place in the world, where would you go?』的提示,並編寫乙個列印調查結果的**塊。

places = 

for i in range(10):

place = input('if you could visit one place in the world, where would you go?')

print(','.join(places))

Python 程式設計 從入門到實踐

1.官網安裝 3.環境配置 務必選中核取方塊add python to path 4.檢視 啟動python版本的命令 python 執行 print hello python world 5.終端執行x.py檔案 python x.py 7.檢視當前目錄中的所有檔案的命令 dir windows系...

python程式設計 從入門到實踐第3章

第三章 列表簡介 1.列表一般用 表示。2.索引從0而不是1開始。通過將索引指定為 1 可讓python返回最後乙個列表元素。4.可使用方法insert 向列表中插入新元素,insert 索引,元素 5.使用del語句根據索引刪除元素 6.方法pop 可刪除列表末尾的元素,並能再使用先前的列表 7....

python程式設計 從入門到實踐 第4章

第四章 操作列表 1.函式range 生成一系列的數字。2.可使用函式list 將range 的結果直接轉換為列表。如果將range 作為list 的引數,輸出將為乙個數字列表。例 numbers list range 1,6 3.列表解析將for迴圈和建立新元素的 合併成一行,並自動新增新元素。例...