gpt4 book ai didi

python - 问题,在 python 3.7 中构建开关盒

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:02 25 4
gpt4 key购买 nike

我正在尝试在 python 3.7 中开发类似 switch-case 功能的东西,就像其他语言已知的那样。

为此,我在这里使用了本教程:https://jaxenter.com/implement-switch-case-statement-python-138315.html

并从这段代码开始:

class ClassCheckShipping:

def __init__(self):
pass

def __checkAktivweltShipping(self, country):
return "checkShipping für Aktivwelt"

def __checkHoerhelferShipping(self, country):
return "checkShipping für Hörhelfer"

def checkShipping(self, merchant, country):
self.country = country
switcher = {
"Aktivwelt": __checkAktivweltShipping,
"Hörhelfer": __checkHoerhelferShipping
}
func = switcher.get(merchant, lambda: "unbekannter Merchant")
print(func())

不幸的是,我收到以下错误,但我找不到我的错误。

File "M:\Python-Projekte\Wipando-Feeds\CheckShipping.py", line 18, in checkShipping "Aktivwelt": __checkAktivweltShipping, NameError: name '_ClassCheckShipping__checkAktivweltShipping' is not defined

你能给我一个修复这段代码的提示吗?

最佳答案

您必须将self 添加到switcher 中的方法:

switcher = {
"Aktivwelt": self.__checkAktivweltShipping,
"Hörhelfer": self.__checkHoerhelferShipping
}

关于python - 问题,在 python 3.7 中构建开关盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com