【问题标题】:unsupported operand type(s) for ** or pow(): 'str' and 'int' with Python3 [duplicate]** 或 pow() 不支持的操作数类型:Python3 中的“str”和“int”[重复]
【发布时间】:2017-06-07 11:34:11
【问题描述】:

我的 Python3 代码

def ask():

    while True:
        try:
            n = input('Input an integer: ')
        except:
            print ('An error occurred! Please try again!')
            continue
        else:
            break


    print ('Thank you, you number squared is: ', n**2)

如果我想取一个数字的平方,为什么会出错?

unsupported operand type(s) for ** or pow(): 'str' and 'int' 

从命令行没有问题

>>> 3**2
9

【问题讨论】:

  • 你得到了什么错误?
  • 您应该将字符串转换为 int n=int(n)n=int(input('Input an integer: '))
  • 我所看到的关于 python2 和 3 发生这种情况的最佳解释,解决方案位于 here

标签: python


【解决方案1】:

input 返回一个字符串; ** 需要 2 个数字。

【讨论】:

  • 我已经使用python 2 年多了,但我不知道这一点。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 2017-04-25
  • 1970-01-01
  • 2013-12-24
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多