在运行模式下出现无效的语法错误,但在调试模式下运行时,运行正常。

问题描述 投票:0回答:1

我是一个编码新手,上个月才开始,我检查了一下问题,但我找不到任何问题,然后我决定用调试的方式运行,结果成功了。

这是我的代码。

class PB_stock:

    def __init__ (self, flavor, amount, old):
        self.flavor = flavor
        self.amount = int(amount)
        self.total = int(amount) + int(old)


choco_stock = PB_stock('choco', 500000, 10) 

print(choco_stock.total)

我试着把代码拆解开来寻找问题所在 但用这个还是不行。

python debugging syntax
1个回答
0
投票

在我看来,这是一个缩进错误,因为你的... def __init__ 内声明的。class PB_stock: 像这样用4个空格缩进。

class PB_stock:

    def __init__ (self, flavor, amount, old):
        self.flavor = flavor
        self.amount = int(amount)
        self.total = int(amount) + int(old)

这样应该就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.