我需要z在 Python 中與 MATLAB 中的索引值相同,我認為這意味著 Matlab 中的 991 = Python 中的 990。
原始 MATLAB 代碼 ( z = 991 )
z = 1;
for i = 1:15
test_timer1 = 1;
for tester1 = 1:12
test_timer2 = 1;
for tester2 = 1:12
if test_timer2 > test_timer1
z = z 1;
end
test_timer2 = test_timer2 1;
end
test_timer1 = test_timer1 1;
end
end
我的 Python 代碼(z = 1980)
z=0
for i in range(15):
test_timer1 = 0
for tester1 in range(12):
test_timer2 = 0
for tester2 in range(12):
if test_timer2 > test_timer1:
z = 1
test_timer2 = 1
test_timer1 = 1
為什么我z的金額是雙倍的?我的錯誤在哪里?謝謝!
uj5u.com熱心網友回復:
您的最后一行(test_timer1 = 1)需要縮進另一個步驟。現在它在你的第一個 for 回圈中 - 你想要它在你的第二個回圈中。
在 matlab 中,這無關緊要,因為您有 end 陳述句表示回圈塊的結束。在 Python 中,縮進用于標記塊。
所以它應該看起來像:
z = 0
for i in range(15):
test_timer1 = 0
for tester1 in range(12):
test_timer2 = 0
for tester2 in range(12):
if test_timer2 > test_timer1:
z =1
test_timer2 = 1
test_timer1 = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511577.html
