Reputation: 25
This is a question that I have been tasked with, I am fairly new to Python so I am struggling a bit enter image description here
(I only need to do 8 horses)
This is my code so far:
horse_info = [['A',11,12],['B',17,7],['C',14,28],['D',15,10],['E',18,30],['F',13,3],['G',16,18],['H',12,23]]
max_h = int(input('Maximum height: '))
max_a = int(input('Maximum age: '))
for i in range(len(horse_info)):
if int(max_h) <= horse_info[i[i]] and int(max_a) <= horse_info[i[i]]:
print('yes')
But I am getting this error:
> Traceback (most recent call last): File
> "/Users/MattDaGama/Documents/Q43.py", line 7, in <module>
> if int(max_h) <= horse_info[i[i]] and int(max_a) <= horse_info[i[i]]: TypeError: 'int' object is not subscriptable
Would appreciate any help :)
EDIT:
I think I have figured out the if statement but I am not sure how to make the print statement.
horse_info = [['A',11,12],['B',17,7],['C',14,28],['D',15,10],['E',18,30],['F',13,3],['G',16,18],['H',12,23]]
max_h = int(input('Maximum height: '))
max_a = int(input('Maximum age: '))
for i in range(len(horse_info)
if int(max_h) <= horse_info[i][1] and int(max_a) <= horse_info[i][2]:
print(horse_info[i][1,2,3])
When I try and run the code it just doesn't print anything.
Upvotes: 1
Views: 104
Reputation: 853
The problem is that when you iterate over a range, the i is an integer. The integer data type is not subscriptable (i.e. you can't do the same thing you can with a list[i] for instance to jump to the index of a list. Ints don't have indexes.
So as a starting place:
if int(max_h) <= horse_info[i[i]] and int(max_a) <= horse_info[i[i]]:
Should be -
if int(max_h) <= horse_info[i][1] and int(max_a) <= horse_info[i][2]:
Assuming horse_info[0][1] = 11
and horse_info[0][2] = 12
based on the variable declaration in the question, (i.e. horse_info = [['A',11,12], ... ]
You have a list of lists, and i is the index of the outer list you want to work with, and the second set of [] are the index of the value you want to work with
Upvotes: 1