3 回答

TA貢獻1793條經驗 獲得超6個贊
該代碼將無法滿足您的要求。
相反,請嘗試此代碼。
import numpy as np
def createlist(r1):
for n in range(1,500):
print(np.arange(r1, r1+n, 1))
r1 = 1
#print(createlist(r1))
createlist(r1)

TA貢獻1934條經驗 獲得超2個贊
問題是因為縮進。
您在您的函數中聲明r1。它會給你錯誤,因為print(createlist(r1))看不到r1. 所以解決方案是:
import numpy as np
def createlist(r1):
for n in range(1,500):
return np.arange(r1, r1+n, 1)
r1 = 1
print(createlist(r1))
希望對你有幫助 <3

TA貢獻1777條經驗 獲得超3個贊
我認為你想要正確縮進你的代碼并且你想要返回每次迭代的結果,你可以嘗試這個
import numpy as np
def createlist(r1):
op = []
for n in range(1, 500):
op.append(list(np.arange(r1, r1 + n, 1))) # op is storing the list generated in each iteration
return op # now you can return op, a list which contains results from the loop
r1 = 1
print(createlist(r1))
ps 請對您想要執行的操作進行更多說明。
添加回答
舉報