【问题标题】:TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'list'类型错误:** 或 pow() 不支持的操作数类型:'int' 和 'list'
【发布时间】:2017-10-10 15:03:52
【问题描述】:

有人可以帮我修复这个 TypeError 吗?我正在尝试将 5 提高一个数字 n 并打印其最后 2 位数字

     from sys import stdin, stdout
     n = [int(x) for x in stdin.readline().rstrip().split()]
     l = 5**n
     res = str(l)
     stdout.write(res(2)+res(1))

【问题讨论】:

  • 如果您要对 One 号码执行此操作,为什么要使用 split() 和列表?
  • 查看错误。它说什么?现在看看5**nn 是一个列表。您不能将列表用作指数。你需要问问自己你想做什么。你也许想要[5**v for v in n]?或者5**n[0]?还是别的什么?

标签: python typeerror python-3.6


【解决方案1】:

我相信这就是你想要做的:

#!/usr/bin/env python3.6
from sys import stdin, stdout


numbers = [int(x) for x in stdin.readline().rstrip().split()]
res = [5**x for x in numbers]
stdout.write(str(res[1] + res[0]))

这从stdin 获取输入并将其拆分为ints 的列表。然后它创建一个新列表,其中包含 5 个提升到 x 的列表,其中 x 是旧列表中的每个值。最后我们将第一个和第二个索引的值加在一起写入stdout。请注意,如果输入的数字少于 2 个,这将引发错误。

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多