【问题标题】:Error in Python when using VPython使用 VPython 时出现 Python 错误
【发布时间】:2014-05-19 09:14:52
【问题描述】:

当我尝试导入任何内容或尝试修改 numpy 语句以导入 from numpy import arange 以外的任何内容(例如 from numpy import *)时,我遇到了错误,或者添加任何其他导入语句。

我也不能让我的半径随机浮动它说 r = random.uniform(0,2) AttributeError: 'builtin_function_or_method' object has no attribute 'uniform'。

这是我尝试修改导入语句时遇到的错误:

Traceback (most recent call last):
   line 13, in <module>
    rate(5)
TypeError: rate() missing 3 required positional arguments: 'pmt', 'pv', and 'fv'

代码:

from visual import *
from math import cos,sin,pi

from numpy import arange


from random import *

s = sphere(pos=[1,0,0],radius=0.1,color = color.red)
s0 = sphere(pos=[0,0,0],radius=0.25,color = color.green)

for i in arange(0,100,0.1):
    rate(5)
    theta = randint(0,30)
    r = randint(-2,2)
    x = cos(theta)
    y = sin(theta)
    s.pos = [x,y,r]

【问题讨论】:

    标签: python numpy vpython


    【解决方案1】:

    在脚本或程序中使用from &lt;some_library&gt; import * 通常是个坏主意,因为这会将&lt;some_library&gt; 中的一切 导入到当前命名空间中。如果&lt;some_library&gt; 中的任何名称已经存在于当前命名空间中,它们将被重新定义。例如,最好这样做

    import numpy as np
    

    并使用前缀np 访问numpy 命名空间(例如np.arange),或者仅明确导入脚本中所需的内容。例如,要从random 导入randint

    from random import randint
    

    在您的情况下,numpyvisual 都定义了一个名为 rate 的函数。 (请参阅http://vpython.org/contents/docs/rate.htmlhttp://docs.scipy.org/doc/numpy/reference/generated/numpy.rate.html。)显然,当您收到报告的错误时,您已经完成了from numpy import *,因此您的脚本调用的是numpy.rate 而不是visual.rate

    【讨论】:

    • 这解决了它非常感谢甚至不知道我能做到这一点,将 numpy 导入为 np。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2011-10-28
    • 2020-09-04
    • 2022-01-18
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多