在Python中,如何在同一类中的另一个方法中传递一个方法的变量

在Python中,如何在同一类中的另一个方法中传递一个方法的变量,python,class,methods,Python,Class,Methods,我是python新手,试图在我的第一段python代码中找出问题所在。我在方法参数中从用户处获取一个输入temp,我想将该temp与make\u item方法中的另一个变量coven\u temp进行比较。但是我得到一个namererror,该名称temp没有定义。我在这里读了一些帖子,其中提到我必须从一个方法返回值。我退回了它,但没有弄清楚如何继续 class maker: def parameter(self): temp = int (inp

我是python新手,试图在我的第一段python代码中找出问题所在。我在方法
参数
中从用户处获取一个输入
temp
,我想将该
temp
make\u item
方法中的另一个变量
coven\u temp
进行比较。但是我得到一个
namererror
,该名称
temp
没有定义。我在这里读了一些帖子,其中提到我必须从一个方法返回值。我退回了它,但没有弄清楚如何继续

    class maker:
        def parameter(self):
            temp = int (input ('At what temperature do you want to make \n'))
            return temp


        def make_item (self):
            def oven (self):
                oven_temp = 0
                while (oven_temp is not temp):
                    oven_temp += 1
                    print ("waiting for right oven temp")

            oven(self)


    person = maker ()
    person.parameter()
    person.make_item()

把它保存在你的
自我中

class MyMaker:
    def ask_temperature(self):
        self.temp = int(input('Temperature? \n'))

    def print_temperature(self):
       print("temp", self.temp)
试试看:

> maker = MyMaker()
> maker.ask_temperature()
Temperature?
4
> maker.print_temperature()
temp 4

以下内容可以解决您的问题。基本上,您希望将要在方法之间传递的变量存储为类的成员。您可以通过为函数指定
self
参数的属性来完成此操作

 class maker:
    def parameter(self):
        self.temp = int (input ('At what temperature do you want to make \n'))


    def make_item (self):
        def oven ():
            oven_temp = 0
            while (oven_temp is not self.temp):
                oven_temp += 1
                print ("waiting for right oven temp")

        oven()


person = maker ()
person.parameter()
person.make_item()