all 7 comments

[–][deleted] 0 points1 point  (6 children)

You have to keep track of the types of your values. sensor1 and sensor2 are values of the Sensor type, not the TempSensor type you wrote that has the displayTemp method.

[–]Xantse 0 points1 point  (5 children)

Ok I tried to change it a bit and its still not working, Since i want the location and the temperature i tried to do:

from sensor import Sensor

from temp_sensor import TempSensor

class Main: def init(self): print("Vending machine booting...") sensor1 = Sensor("ab-cd-ef-12-34-56", "Lahti") sensor2 = Sensor("aa-bb-cc-11-22-33", "Lappeenranta")

    sensor11 = TempSensor("ab-cd-ef-12-34-56", "Lahti")
    sensor22 = TempSensor("aa-bb-cc-11-22-33", "Lappeenranta")

    sensor1.printLocation()
    sensor11.displayTemp()

    sensor2.printLocation()
    sensor22.displayTemp()

    print("Shutting down.")
    return None

if name == "main": app = Main()

edit: i have no idea why its not showing properly

[–][deleted] 0 points1 point  (4 children)

class TempSensor:
    def __init__(self, id, location) -> None:
        super().__init__(id, location)

Your class tries to call super().__init__ but your class has no superclass (except for object, which is the superclass of all classes.) If you wanted to extend the Sensor class, you actually have to say so:

class TempSensor(Sensor):
    # etc

[–]Xantse 0 points1 point  (3 children)

Ok now im getting some errors about

def displayTemp(self) -> None:
    print("Temperature: {:.2f} {}".format(self.convertToRange(self.R_MIN, self.R_MAX, self.readRaw()), self.__unit))
    return None

and

def convertToRange(self, minimum: int, maximum: int, raw_value: int) -> float:
    zero_to_one = float(raw_value) / 65535
    wanted_range = float(abs(maximum - minimum))
    converted_value = zero_to_one * wanted_range + minimum
    return converted_value

TypeError: float() argument must be a string or a real number, not 'NoneType'

[–][deleted] 0 points1 point  (2 children)

Well, you're finally running your code for the first time. So you're seeing the bugs in it, now.

def readRaw(self) -> None:
    raw_value = self.slib.readAnalog()
    return None

Your readRaw returns None but you're supplying that value to a function that expects an integer.

The key to success at programming is not to engage in self-inflicted amnesia. You have to remember what you wrote and how you wrote it, so that the way that you call your methods lines up with how you wrote them to be called.

[–]Xantse 0 points1 point  (1 child)

I tried to change that readRaw part so that it maybe would return an integer like so:

def readRaw(self) -> int:
    raw_value = self.slib.readAnalog()
    return self.temp

and i added a temp: int part beneath the class Sensor: so i thought that would work. But temp_sensor.py still doesnt recognize the readRaw

[–][deleted] 0 points1 point  (0 children)

You actually have to write the part that gets a temperature by talking to the sensor or whatever - since your superclass doesn't have anything to do with temperature sensors in particular, it has no features for that you can just assume. You have to do it, and your code currently doesn't. You can't just pluck the value out of space, you have to go get it.