为什么这里会出现缩进错误,如何修复?

huangapple go评论94阅读模式
英文:

Why is there an indentation error here and how to fix it?

问题

from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt

# 定义常量
β = 1
δ = 1/5
N = 100000
γ = 1/10
α = 5/100
ρ = 1/15

# 返回 T 时刻的导数函数
def f(y,t):
    S, E, I, R, D = y # 获取 S、E、I、R、D 的先前值并存储在数组 y 中
    d0 = -β*(S/N)*I # S(t) 的导数
    d1 = β*(S/N)*I - δ*E # E(t) 的导数
    d2 = δ*E - (1-α)*γ*I - α*ρ*I # I(t) 的导数
    d3

在这里,我已经将你提供的 Python 代码翻译为中文。请注意,你提供的代码中有一个问题,d3 的导数没有被正确定义,这可能是导致缩进错误的原因之一。你需要为 d3 提供正确的导数定义。

英文:

Hi I barely know anything about python and just copied some code for my school project and it shows an indentation error even though im pretty sure it's indented properly.

    from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt

# DEFINE CONSTANTS
β = 1
δ = 1/5
N = 100000
γ = 1/10
α = 5/100
ρ = 1/15

# FUNCTION TO RETURN DERIVATIVES AT T
def f(y,t):
	S, E, I, R, D = y # get previous values of S, E, I, R, D and store them in array y
	d0 = -β*(S/N)*I # derivative of S(t)
	d1 = β*(S/N)*I - δ*E # derivative of E(t)
	d2 = δ*E - (1-α)*γ*I - α*ρ*I # derivative of I(t)
  d3

When i copy paste the code here it shows d3 as being unindented but in replit it looks like it is indented properly

I tried pressing space and delete to move d3 around but it still showed an indentation error

here it looks like its indented

答案1

得分: 0

在运行您的代码后,出现了第1行和第19行的错误。
第一行的错误是因为您有4个空格(似乎没有明显原因)。
第19行的错误是因为您缺少了2个空格,并且没有定义变量d3。

以下是更新后的代码:

from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt

# 定义常量
β = 1
δ = 1/5
N = 100000
γ = 1/10
α = 5/100
ρ = 1/15

# 返回在T时刻的导数的函数
def f(y,t):
    S, E, I, R, D = y # 获取S、E、I、R、D的先前值,并将它们存储在数组y中
    d0 = -β*(S/N)*I # S(t)的导数
    d1 = β*(S/N)*I - δ*E # E(t)的导数
    d2 = δ*E - (1-α)*γ*I - α*ρ*I # I(t)的导数
    d3 = 0 # 需要定义d3

注意:我在第19行中添加了d3的定义,将其初始化为0。

英文:

After running your code, you had an error with line 1 & line 19.
Line one error is due to the fact that you had 4 spaces (for no apparent reason).
Line 19 is due to the fact that you were missing 2 spaces & you haven't defined the variable d3.

Here's the updated code:

from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt

# DEFINE CONSTANTS
β = 1
δ = 1/5
N = 100000
γ = 1/10
α = 5/100
ρ = 1/15

# FUNCTION TO RETURN DERIVATIVES AT T
def f(y,t):
    S, E, I, R, D = y # get previous values of S, E, I, R, D and store them in array y
    d0 = -β*(S/N)*I # derivative of S(t)
    d1 = β*(S/N)*I - δ*E # derivative of E(t)
    d2 = δ*E - (1-α)*γ*I - α*ρ*I # derivative of I(t)
    d3 #<- needs to be defined

huangapple
  • 本文由 发表于 2023年7月3日 10:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定