python程式設計 從入門到實踐 第七章練習題

2021-08-17 16:32:24 字數 2448 閱讀 1140

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

num = int(input("please input a number: "))

if num % 10 == 0:

print(num,'is an integer multiple of 10')

else:

print(num,"isn't an integer multiple of 10")

in [2]: runfile('e:')

please input a number: 12

12 isn't an integer multiple of 10

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

中新增這種配料。

while true:

ingre = input("please input an ingredient to pizza: ")

if ingre == 'quit':

break

else:

print('we will add this ingredient to the pizza')

in [3]: runfile('e:')

please input an ingredient to pizza: egg

we will add this ingredient to the pizza

please input an ingredient to pizza: quit

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

結果的**塊。

responses = {}

active = true

while active:

name = input("what is your name? ")

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

responses[name] = response

while true:

repeat = input('would you like to get another response? (y/n) ')

if repeat.lower() == 'n':

active = false

break

elif repeat.lower() == 'y':

active = true

break

print('--- poll results ---')

for name, response in responses.items():

print(name + ' like to visit ' + response + '.')

in [4]: runfile('e:')

what is your name? hujie

if you could visit one place in the world, where would you go? beijing

would you like to get another response? (y/n) j

would you like to get another response? (y/n) k

would you like to get another response? (y/n) y

what is your name? huer

if you could visit one place in the world, where would you go? nanjing

would you like to get another response? (y/n) y

what is your name? kl

if you could visit one place in the world, where would you go? xi'an

would you like to get another response? (y/n) n

--- poll results ---

hujie like to visit beijing.

huer like to visit nanjing.

kl like to visit xi'an.

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迴圈和建立新元素的 合併成一行,並自動新增新元素。例...