【发布时间】:2018-02-22 09:36:30
【问题描述】:
这是我的任务,我对 Python 还很陌生,所以我有点挣扎enter image description here
(我只需要做8匹马)
这是我目前的代码:
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')
但我收到此错误:
> 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
希望有任何帮助:)
编辑:
我想我已经弄清楚了 if 语句,但我不确定如何制作 print 语句。
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])
当我尝试运行代码时,它不会打印任何内容。
【问题讨论】:
-
i是一个整数......所以i[i]没有意义......你可能只是说horse_info[i]- 尽管你不需要在Python中通过索引来访问东西.. . 你可以直接循环它们。 -
可能是
horse_info[i][1]和horse_info[i][2]。 -
我希望它进入每个子列表并比较它们
-
@Matthew Valetsky。该问题已在下面提出并回答。下面的答案都回答了你的第一个和第二个问题。我很高兴根据您的要求进行编辑
标签: python