【问题标题】:What is wrong with my code for project Euler which throws an unsupported operand type error我的 Euler 项目代码有什么问题,它引发了不受支持的操作数类型错误
【发布时间】:2015-09-26 22:45:03
【问题描述】:
numbers = [1, 2]
i = 0
while i < 4000000:
    x = int(len(numbers - 1))
    new = numbers[x] + numbers[x-1]
    if new % 2 == 0:
    numbers.append(new)
    i = new
print sum(numbers) - 1

错误提示:

Traceback (most recent call last):
  File "python", line 4, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'int'

【问题讨论】:

  • 大括号可能在错误的位置:int(len(numbers)- 1)
  • 如果您使用编程语言进行标记,则潜在的回答者更容易找到相关的问题来回答
  • len(numbers) 为您提供列表的长度。但是 len(numbers-1) 无效。

标签: python math fibonacci


【解决方案1】:

Numbers 是一个数组,不能从数组中减去整数。你可能想要

x = int(len(numbers) - 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2018-12-26
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多