我想将一些值输入到2D数组中。
当我输入arr[days][count]
时,它适用于第一部分,但是当我第二次输入days>1
时,它似乎无法正常工作。
例如,天数为3,数字为2,我只能将值输入arr[1][1]
,而不是arr[2][0]
及以后。
arr = [[0 for row in range(week+1)] for column in range(numcows)]
for days in range(1,week+1):
count=0
for count in range(numcows):
while days == 1:
arr[0][count] = float(input ("Enter cow ID"))
...
arr[days][count] = daytotal
count = count+1
break
while days>1:
for count in range(numcows):
....
arr[days][count] = daytotal #doesn't work
count = count+1
days = days+1
我的代码出了什么问题?
答案 0 :(得分:0)
我不确定你想要完成什么,bur看起来像你想要创建一个2D数组,其中第一行如果牛ID和下一个n-1行是n-1周内的奶牛数量。如果是这种情况,上面写的代码似乎不正确。
我编写了一个代码,用于为所有用户输入的2X3数组添加值,并且工作正常 -
week=2
numcows=3
arr = [[0 for row in range(week+1)] for column in range(numcows)]
for days in range(1,week+1):
for count in range(numcows):
if days == 1:
arr[0][count] = float(input ("Enter cow ID"))
arr[days][count] = float(input ("Number of cows on Day " + str(days)))
if days>1:
arr[days][count] = float(input ("Number of cows on Day " + str(days)))