I am trying to open a csv file in python and read the rows into a list.
The list is then sent to a function where I want to add 5 to the value of x after each iteration with the value of d going up by 10 after every 10th iteration.
d I have working but can’t quite figure out x. Please can someone help me understand where I am going wrong with my math. I thought setting x = 0 outside the loop and then x+=5 inside would do it.
JavaScript
x
77
77
1
results = []
2
with open('file.csv', newline='') as inputfile:
3
for row in csv.reader(inputfile):
4
results.append(row[0]) # 24 rows in csv file
5
6
def buildcount(listGroup, z):
7
listGroup = listGroup
8
z = z
9
x = 0
10
for i in range (0, len(listGroup))
11
print(i)
12
d = 10*(i // 10 + z)
13
x +=5
14
print(x)
15
if i % 10 == 0:
16
x = 0
17
return i
18
19
z = 10
20
mainInput = results
21
buildcount(mainInput)
22
23
24
25
#current output of x
26
5
27
5
28
10
29
15
30
20
31
25
32
30
33
35
34
40
35
45
36
5
37
10
38
15
39
20
40
25
41
30
42
35
43
40
44
45
45
5
46
10
47
15
48
20
49
25
50
51
52
#desired output of x
53
0
54
5
55
10
56
15
57
20
58
25
59
30
60
35
61
40
62
45
63
0
64
5
65
10
66
15
67
20
68
25
69
30
70
35
71
40
72
45
73
0
74
5
75
10
76
15
77
Advertisement
Answer
It looks like you’re looking for some fairly simple math on the loop counter:
JavaScript
1
7
1
def buildcount(listGroup):
2
for i in range (0, len(listGroup)):
3
x = (i % 10) * 5
4
d = (i // 10) * 10
5
print(f'i={i}, x={x}, d={d}')
6
return i
7
Output (for a 24-entry listGroup
):
JavaScript
1
25
25
1
i=0, x=0, d=0
2
i=1, x=5, d=0
3
i=2, x=10, d=0
4
i=3, x=15, d=0
5
i=4, x=20, d=0
6
i=5, x=25, d=0
7
i=6, x=30, d=0
8
i=7, x=35, d=0
9
i=8, x=40, d=0
10
i=9, x=45, d=0
11
i=10, x=0, d=10
12
i=11, x=5, d=10
13
i=12, x=10, d=10
14
i=13, x=15, d=10
15
i=14, x=20, d=10
16
i=15, x=25, d=10
17
i=16, x=30, d=10
18
i=17, x=35, d=10
19
i=18, x=40, d=10
20
i=19, x=45, d=10
21
i=20, x=0, d=20
22
i=21, x=5, d=20
23
i=22, x=10, d=20
24
i=23, x=15, d=20
25