3

I am taking an input temp from user in method parameter and I want to compare this temp with another variable oven_temp in the make_item method. But I get a NameError, saying that name temp is not defined.

I read some posts here and I understood that I need to return the value from the method - so I did that; but now what? Specifically, I tried:

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

but I still get the NameError. How do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
learningpal
  • 309
  • 1
  • 8
  • 17

2 Answers2

4

The following should work to solve your problem. Basically, you want to store the variables you want to pass between methods as members of the class. You do this by assigning an attribute of the self parameter to the functions.

 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()
Anthony Oteri
  • 402
  • 4
  • 14
2

Keep it in yourself!

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

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

Try it:

> maker = MyMaker()
> maker.ask_temperature()
Temperature?
4
> maker.print_temperature()
temp 4
salezica
  • 74,081
  • 25
  • 105
  • 166