我正在嘗試在 python 中打開一個 csv 檔案并將行讀入串列。
然后將該串列發送到一個函式,在該函式中,我想在每次迭代后將 x 的值加 5,而 d 的值在每 10 次迭代后增加 10。
d 我有作業,但不太清楚 x。請有人可以幫助我了解我的數學哪里出了問題。我認為在回圈外部設定 x = 0 然后在內部設定 x =5 就可以了。
results = []
with open('file.csv', newline='') as inputfile:
for row in csv.reader(inputfile):
results.append(row[0]) # 24 rows in csv file
def buildcount(listGroup, z):
listGroup = listGroup
z = z
x = 0
for i in range (0, len(listGroup))
print(i)
d = 10*(i // 10 z)
x =5
print(x)
if i % 10 == 0:
x = 0
return i
z = 10
mainInput = results
buildcount(mainInput)
#current output of x
5
5
10
15
20
25
30
35
40
45
5
10
15
20
25
30
35
40
45
5
10
15
20
25
#desired output of x
0
5
10
15
20
25
30
35
40
45
0
5
10
15
20
25
30
35
40
45
0
5
10
15
uj5u.com熱心網友回復:
看起來您正在回圈計數器上尋找一些相當簡單的數學:
def buildcount(listGroup):
for i in range (0, len(listGroup)):
x = (i % 10) * 5
d = (i // 10) * 10
print(f'i={i}, x={x}, d={d}')
return i
輸出(對于 24-entry listGroup):
i=0, x=0, d=0
i=1, x=5, d=0
i=2, x=10, d=0
i=3, x=15, d=0
i=4, x=20, d=0
i=5, x=25, d=0
i=6, x=30, d=0
i=7, x=35, d=0
i=8, x=40, d=0
i=9, x=45, d=0
i=10, x=0, d=10
i=11, x=5, d=10
i=12, x=10, d=10
i=13, x=15, d=10
i=14, x=20, d=10
i=15, x=25, d=10
i=16, x=30, d=10
i=17, x=35, d=10
i=18, x=40, d=10
i=19, x=45, d=10
i=20, x=0, d=20
i=21, x=5, d=20
i=22, x=10, d=20
i=23, x=15, d=20
uj5u.com熱心網友回復:
看起來您想x在 50 后重置,然后在達到 10 的倍數后增加 d。第一個使用 mod ( %),第二個使用整數除法。
x = 0
d = 0
for i in range(21):
x = i*5 % 50
d = int(i/10) * 10
print(f'{i=} {x=} {d=}')
回傳
i=0 x=0 d=0
i=1 x=5 d=0
i=2 x=10 d=0
i=3 x=15 d=0
i=4 x=20 d=0
i=5 x=25 d=0
i=6 x=30 d=0
i=7 x=35 d=0
i=8 x=40 d=0
i=9 x=45 d=0
i=10 x=0 d=10
i=11 x=5 d=10
i=12 x=10 d=10
i=13 x=15 d=10
i=14 x=20 d=10
i=15 x=25 d=10
i=16 x=30 d=10
i=17 x=35 d=10
i=18 x=40 d=10
i=19 x=45 d=10
i=20 x=0 d=20
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/510412.html
上一篇:如何迭代R中的函式?
下一篇:倒計時回圈猜謎游戲的機會
