看板 Python 關於我們 聯絡資訊
※ 引述《yungyuc (酷狗喵千代)》之銘言: : ※ 引述《azulazure (azul)》之銘言: : : 如果說有一個像這樣的語法 : : counter = 0 : : for item in [0, 1, 2]: : : counter += item : : print counter : : 這樣是可以得到結果 : : 但如果我想要在之後再次使用這個數字怎麼辦? : : 我要如何設定一個variable, i.e., "num" : : 使得num = "counter += item" ?? : : 一副就是很新手的問題... : 其實我看不太懂你的問題 :p : 如果有什麼東西之後還要用,就該取個名字: : counter = 0 : items = [0,1,2] : for item in items: : counter += item : print counter, items 抱歉 那我再解釋一下 如果我是因為下面這樣的原因 total = 100 counter = 0 for item in [0, 1, 2]: counter += item print item, total/counter 不知道這樣有沒有解釋到 因為我要在同一個for loop底下做計算 但是這樣counter並不是一個固定的數字(對吧??) 因為產生出來的並不是我要的output 所以我才想知道有沒有辦法把它取一個名字 或是說如果我要在for loop底下用這個variable做計算的話 有沒有什麼辦法呢 因為我希望在我的output裡 每一個item之後都能列出這個數字 希望這樣有解釋到 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 69.143.181.102
weijr:雖然我大概猜得到你在想什麼,但我覺得,如果你試著把你想 05/06 12:00
weijr:表達的東西用中文說清楚,你就會寫了 05/06 12:00
weijr:只要說清楚「這個數字」是什麼,就能寫出你要的程式 05/06 12:02
godfat:我還是看不懂 @@ 05/06 13:37
godfat:你想替誰取名字?他不就叫 counter 嗎? 05/06 13:38
yoco315:完全看不懂你想表達的意思 05/06 14:36
azulazure:never mind, I think I figured it out 05/06 21:34
azulazure:thanks for all your replies 05/06 21:35
azulazure:I'm not good at writing down my question 05/06 21:35
ibmibmibm:用兩次的for阿 05/07 02:49
azulazure:that's a good way, thanks!!! 05/07 05:38