I need z
to be the same index value in Python as in MATLAB, which I think means 991 in Matlab = 990 in Python.
The original MATLAB code (z = 991)
JavaScript
x
22
22
1
z = 1;
2
for i = 1:15
3
test_timer1 = 1;
4
5
for tester1 = 1:12
6
test_timer2 = 1;
7
8
for tester2 = 1:12
9
10
if test_timer2 > test_timer1
11
z = z+ 1;
12
end
13
14
test_timer2 = test_timer2 + 1;
15
end
16
17
test_timer1 = test_timer1 + 1;
18
19
end
20
21
end
22
My Python code (z = 1980)
JavaScript
1
16
16
1
z=0
2
for i in range(15):
3
test_timer1 = 0
4
5
for tester1 in range(12):
6
test_timer2 = 0
7
8
for tester2 in range(12):
9
10
if test_timer2 > test_timer1:
11
z += 1
12
13
test_timer2 += 1
14
15
test_timer1 += 1
16
Why is my z
double the amount? Where is my error?
Thanks!
Advertisement
Answer
Your last line (test_timer1 += 1)
needs to be indented another step. Right now it’s in your first for loop – you want it in your second.
In matlab, this doesn’t matter because you have the end statement signifying the end of your loop block. In Python, indentation is used to mark blocks.
So it should look like:
JavaScript
1
11
11
1
z = 0
2
for i in range(15):
3
test_timer1 = 0
4
for tester1 in range(12):
5
test_timer2 = 0
6
for tester2 in range(12):
7
if test_timer2 > test_timer1:
8
z+=1
9
test_timer2 += 1
10
test_timer1 += 1
11