python學習日誌 3

2021-10-08 01:23:47 字數 3114 閱讀 3339

6月24日

1.重量單位轉換器 (l(磅)<—> k(千克))

weight = input('insert weight: ')

unit = input ('l(lbs) or k(kg): ')

if unit.upper() == 『l』:

weight_new = 0.45 * float(weight)

print(f』your weight(kg) is: 』)

elif unit == 『k』 or unit == 『k』:

weight_new = float(weight) / 0.45

print(f』your weight(lbs) is: 』)

else:

print(『invalid insert』)

執行結果:

insert weight: 120

l(lbs) or k(kg): l

your weight(kg) is: 54.0

insert weight: 120

l(lbs) or k(kg): k

your weight(lbs) is: 266.6666666666667

while迴圈 : 在限定條件內,將一段特定**執行多次

#guessing game #猜數字遊戲,3次機會,猜中9獲勝

secret_number = 9 #正確答案

guess_count = 0 # 計數器

guess_limit = 3 #次數限制

while guess_count < guess_limit: while 迴圈

guess = int(input('insert a number: '))

guess_count += 1

if guess == secret_number:

print(『you won !』)

break #若輸入的input是正確答案,則在此處跳出迴圈,(無論3次機會是否用完,都不再進行後續輸入了)

else:

print(『sorry, you failed !』)

3.#car game

title = 』 』

while true:

title = input(』> ').lower( )

if title == 『help』:

print("""

start - to start the car

stop - to stop the car

quit - to quit

「」")

elif title == 『start』:

print(f』car started . . . ready to go !!!』)

elif title == 『stop』:

print(f』car stopped !!』)

elif title == 『quit』:

print(f』game over !』)

break

else:

print(f"sorry, i don』t understand it…")

#car game 改進版,若重複輸入 則顯示提示資訊

title = 』 』

started = false 用boolean值來代表汽車的狀態(started or not)

while true: 「 while true 」 會讓while迴圈一直執行下去

title = input(』> ').lower()

if title == 『help』:

print("""

start - to start the car

stop - to stop the car

quit - to quit

「」")

elif title == 『start』:

if started:

print(『hey, the car is already started !』)

else:

print(f』car started . . . ready to go !!!』)

started = true

elif title == 『stop』:

if not started:

print(f』hey dude, the car is already stopped, what are you doing !!』)

else:

print(f』car stopped !』)

started = false

elif title == 『quit』:

print(f』game over !』)

break

else:

print(f"sorry, i don』t understand it…")

5.for迴圈 #for loops :將乙個序列中的值依次執行

利用for迴圈將price中的值求和

price = [10,20,30]

add = 0

for item in price:

add += item

print(add)

#nested loops 巢狀迴圈

for x in range(4):

for y in range(3):

print(f』(,)』)

執行結果:

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

(3,0)

(3,1)

(3,2)

例:利用nested loop 列印出乙個「l」

#nested loops

numbers = [2,2,2,2,5]

for x_count in numbers:

output = 』 』

for stars in range(x_count):

output = output + 「x」

print(output)

執行結果:

***x

***x

***xx

學習日誌3

學習日誌 姓名 孔令斌 日期 2018.7.11 今日學習任務 陣列今日任務完成情況 詳細說明本日任務是否按計畫完成,開發的 量 今日任務成功完成,我們完成了指標 今日開發中出現的問題彙總 不能完全弄懂指標變數的各種運用,相應的知識點有些匱乏,需要補充。今日未解決問題 指標變數的各種變化用法 今日開...

學習日誌3

蘇嵌 專案實戰 學習日誌 姓名 陳晨 日期 2018.9.5 今日學習任務 佇列的順序 鏈式儲存。自定義函式來實現初始化,清空,輸入,輸出,刪除,銷毀等一系列操作 今日任務完成情況 main.c include include queue.h include int main else if ret...

學習日誌3

學習日誌3 姓名 高昱朋 今日學習任務 1.順序儲存 2.鏈式儲存 今日任務完成情況 成功按計畫完成學習任務 開發 500行左右 今日開發中出現的問題彙總 1.迴圈佇列的概念不清淅 2.不知道如何判斷佇列是否滿 3.對兩種儲存方式不能分清 今日未解決問題 1.今天知識點比較多,沒有時間去吸收 2.一...