I want to input some values into a 2D array.
It works for the first part when I input into arr[days][count]
, however, when I input it the second time when while is days>1
, it seems like it doesn't work.
For example, is days is 3 and numcows is 2, I can only input values into arr[1][1]
not to arr[2][0]
and onwards.
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
What is wrong with my code?
I am not sure what you want to accomplish, bur looks like you want to create a 2D array, in which first row if cow ID and next n-1 rows are numbers of cows in n-1 weeks. If that is the case, the code written above seems incorrect.
I have written a code to add values to a 2X3 array with all user inputs and it works fine -
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)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.