【发布时间】: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