python-2.7 - Python : “SyntaxError: invalid syntax” for arithmetic

标签 python-2.7 syntax-error

这是我关于堆栈溢出的第一个问题,请评论如何改进问题。

我觉得这是我代码的相关部分。

#import libraries
from numpy import *
from matplotlib.pyplot import *

#leapfrog method
def orbit(m1=1.0e30, m2=1.0e24, a=1.0e11, e=0.1):

    #conditions
    G=6.67*e-11
    q=m1/m2
    m=m1+m2
    r0=(1-e)*a/(1+q)
    v0=1/(1+q)sqrt((1+e)/(1-e)*G*m/a)

导入代码时收到错误消息:
import hw5redo
  File "hw5redo.py", line 12
    r0 = (1-e)*a/(1+q)
                   ^
SyntaxError: invalid syntax

另外,当我从代码中注释掉它时,我也会收到:
import hw5redo
  File "hw5redo.py", line 12
    #r0=(1-e)*a/(1+q)
                 ^
SyntaxError: invalid syntax

我认为注释甚至不应该被视为语法错误,因为它们仅用于用户。

最佳答案

@rth是正确的,请检查下面的代码,您应该在sqrt函数之前使用*:

def orbit(m1=1.0e30, m2=1.0e24, a=1.0e11, e=0.1):

    G = 6.67 * e - 11
    q = m1 / m2
    m = m1 + m2
    r0 = (1 - e) * a / (1 + q)
    value = (1 + e) / (1 - e) * G * m / a
    # Note that value is negative in this case, and it will raise a warning
    print value
    v0 = 1 / (1 + q) * sqrt(value)

希望代码对您有所帮助。

关于python-2.7 - Python : “SyntaxError: invalid syntax” for arithmetic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527629/

相关文章:

python - 为什么我不能在 wxPython 中销毁我的 StaticText?

python-2.7 - 如何在 kivy 中创建超链接?

python - 从 virtualenv 环境中的源 tarball 构建的可重定位(独立)Python?

bash - 为什么这个简单的 bash 代码会出现语法错误?

mysql - sql update with inner join 和where

python - 安装和运行 PYDOE 的问题

python - 格式化包含非 ascii 字符的列

python - 理解 Python 类和对象

android - SQLite:表 [name_table] 没有名为 [name_column] 的列

sql - '='附近的语法不正确