python类变量定义问题
我刚开始学习课程,主要是通过阅读这里 我也尝试在类之间进行分支,但出于某种原因 当我到达结果班时 我得到一个错误,没有定义成本。。 有人能告诉我我做错了什么吗python类变量定义问题,python,python-3.x,oop,Python,Python 3.x,Oop,我刚开始学习课程,主要是通过阅读这里 我也尝试在类之间进行分支,但出于某种原因 当我到达结果班时 我得到一个错误,没有定义成本。。 有人能告诉我我做错了什么吗 class calculator(): def money(self): print("What was the cost?") cost = input("> ") diners.amount_of_diners() class diners(): def amo
class calculator():
def money(self):
print("What was the cost?")
cost = input("> ")
diners.amount_of_diners()
class diners():
def amount_of_diners():
print("How many people are with you?")
diners = input("> ")
precent.precent_to_give()
class precent():
def precent_to_give():
print("How much '%' you want to give the waiter? ")
prec = input("> ")
result.the_end()
class result():
def the_end():
print("The total amount of money each of you need have to give is: ",cost * diners)
calc = calculator()
calc.money()
Cost是money()的局部变量,result()无法访问它。这是因为变量“Cost”和“diners”不是全局变量(它们只是类的局部变量)。要将它们定义为全局变量,您需要将它们定义为全局变量,因此您的代码应该如下所示:
class calculator():
def money(self):
print("What was the cost?")
global cost
cost = int(input("> "))
diners.amount_of_diners()
class diners():
def amount_of_diners():
print("How many people are with you?")
global diners
diners = int(input("> "))
precent.precent_to_give()
class precent():
def precent_to_give():
print("How much '%' you want to give the waiter? ")
#global prec #you can uncomment this to also include prec as a global variables if you want to do other equations with it as well
prec = input("> ")
result.the_end()
class result():
def the_end():
result = cost*diners
print("The total amount of money each of you need have to give is: "+str(result))
calc = calculator()
calc.money()
在字符串中添加int也有问题,所以我也为您解决了这个问题
另一种更有效的方法是使用以下代码:
class calculator:
cost=0
def money(self):
print("What was the cost?")
calculator.cost = int(input("> "))
diners.amount_of_diners()
class diners:
diners=0
def amount_of_diners():
print("How many people are with you?")
diners.diners = int(input("> "))
precent.precent_to_give()
class precent:
prec=0
def precent_to_give():
print("How much '%' you want to give the waiter? ")
precent.prec = input("> ")
result.the_end()
class result:
def the_end():
result = calculator.cost * diners.diners
print("The total amount of money each of you need have to give is: "+str(result))
calc = calculator()
calc.money()
这样做效果更好,因为它仍然使每个变量都是类的局部变量,并且只从类外部调用变量;而不是将它们全部作为全局变量。结果中的print语句。end类方法试图打印两个变量的总和,这两个变量都未在函数范围内或函数可以看到的范围内定义。从编写代码的方式来看,似乎您假设它应该从
计算器.money
类方法中了解变量成本
,从就餐者.amount\u就餐者
类方法中了解变量就餐者
这不是怎么回事。这些变量绑定到定义它们的范围。没有理由期望这些变量在其他类中可见
当然,解决方案将涉及通过某种方式(传递参数、全局范围等)使这些变量在
结果
类中可见。然而,您实际上并没有按照使用Python类的方式使用它们——在我看来,您将它们视为某种准函数。也就是说,您的程序试图解决的问题并不一定首先需要类——使用简单函数更合适。如果您坚持使用类来解决这个问题,我的第一直觉告诉我,您不应该需要多个类来实现所需的行为。如果我是你,我会多学一些。当我很难为一个类找到一个合适的名称,或者决定它应该公开什么样的功能时,我会编写一些伪代码来模拟我希望使用我的潜在类编写的代码。那么我该如何修复它呢(michox2,我想知道你如何在我的回答中实现这一点!好吧,我完成了编辑,它现在对你有效吗?因为它似乎对我非常有效!它将一如既往地有效,没有“自我”-但这并不意味着我们应该使用类-在不创建实例的情况下,Python中的无参数方法只是一个普通函数,恰好位于类主体创建的名称空间中。但是,这可能是由知道在做什么的人故意做的,根本不是使用类的正确方式,更不用说学习它们应该如何工作了。也就是说,问题中给出的示例问题没有帮助,因为它无论如何都不会映射到类/实例。是的,我明白你的意思,我会解决它的,jsbueno!谢谢你的反馈!顺便说一句:)jsueno我完成了编辑并包含了第二段代码,现在是更好了,还是还有其他方面需要改进?如果您按照预期使用它们,类会更容易理解。这些类没有任何存在的理由,因为它们不封装任何数据。