【问题标题】:TypeError from attempted exponentiation来自尝试求幂的 TypeError
【发布时间】:2018-04-07 01:53:06
【问题描述】:

我试图将在二维和三维氢原子的基态中找到电子的概率绘制为 r 的函数。我现在的代码是:

import math
import matplotlib.pyplot as plt
import numpy as np

def three_dimensional(radius):
    bohr = (5.2917721067)*10**(-11)
    use_radius = []
    for i in radius:
        new_rad = bohr*i
        use_radius.append(new_rad)

    answers = []
    for i in use_radius:
        R_r = (2//(bohr)**(3//2))*math.exp(-i/bohr)
        answers.append(R_r)

    probability = []
    for i in answers:
        probs = i^2
        probability.append(probs)

    print(answers)

    return plt.contour(answers, probability)

我收到错误:

TypeError: ^: 'float' 和 'int' 的操作数类型不受支持

解决此问题的最佳方法是什么?

【问题讨论】:

    标签: operators python-3.4 exponentiation


    【解决方案1】:

    您需要 ** 运算符,它是 Python 中的指数运算符,而不是 ^。所以这一行应该是:

    for i in answers:
        probs = i**2
        probability.append(probs)
    

    【讨论】:

    • 哇,真不敢相信我做到了!太感谢了。现在我的输入有问题,我收到了这个错误消息TypeError: unsupported operand type(s) for ^: 'float' and 'int'
    • 我可以使用更好的绘图功能吗?
    • 这将是一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2013-08-30
    • 2017-03-01
    • 1970-01-01
    • 2015-01-06
    • 2020-11-15
    相关资源
    最近更新 更多