gpt4 book ai didi

python 类变量定义问题

转载 作者:行者123 更新时间:2023-12-01 00:18:43 24 4
gpt4 key购买 nike

我刚刚开始学习类(class),主要是通过阅读这里 https://pythonprogramming.net/classes-python-3-basics-tutorial/

我也尝试在类之间进行分支,但出于某种原因当我到达 result 类时我收到一个错误,指出成本未定义。有人可以告诉我我做错了什么吗?

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() 访问

关于python 类变量定义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59111217/

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