亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您能解釋一下嗎,代碼課程中的一堂課

您能解釋一下嗎,代碼課程中的一堂課

慕村9548890 2021-03-30 17:34:02
我當時在代碼學院學習代碼,當時我在其中編寫代碼,但沒有得到練習的解決方案,我單擊了給我解決方案選項,然后得到了我不理解該程序的解決方案。prices = {"banana": 4,"apple": 2,"orange": 1.5,"pear": 3}stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}for food in prices:  print food  print "price: %s" % prices[food]  print "stock: %s" % stock[food]然后打印出來orangeprice: 1.5stock: 32pearprice: 3stock: 15bananaprice: 4stock: 6appleprice: 2stock: 0你能解釋一下它的過程嗎?它如何打印上面給出的輸出
查看完整描述

2 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

知道這個問題會有所幫助。但是我可以嘗試在不知道問題的情況下從高層次上解釋該程序。


prices = {"banana": 4,"apple": 2,"orange": 1.5,"pear": 3}


stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}

這是兩個字典的定義,第一個字典僅存儲每個水果的價格,第二個字典存儲存儲中的水果數量(庫存)。


for food in prices: ### For loop iterates over the Keys of the dict (fruitnames)

    print food ## printing current key for iteration

    print "price: %s" % prices[food] ## printing price of the fruit

    print "stock: %s" % stock[food] ## printing stock of the fruit.

順便說一下,這看起來像Python2語法,因為該print語句沒有括號。我強烈建議改為學習python3。


查看完整回答
反對 回復 2021-04-20
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

您有兩個字典prices,分別是和stock


prices = {"banana": 4,"apple": 2,"orange": 1.5,"pear": 3}

stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}

你遍歷keys的prices通過字典for food in prices:此行。


閱讀此代碼的注釋:


prices = {"banana": 4,"apple": 2,"orange": 1.5,"pear": 3} #prices dict


stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15} #stock dict


for food in prices: #iterate over the keys of prices dict

  print food #print the key

  print "price: %s" % prices[food] #print the value of prices dict at food key

  print "stock: %s" % stock[food] #print the value of stock dict at food key


查看完整回答
反對 回復 2021-04-20
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號