Python lỗi typeerror read_temp takes no arguments 1 given năm 2024

It says that I’m passing display_car() an argument, but when I take out the “my_car,” it says that display_car isn’t defined! What do I do?

Answer 54d1cebed3292fa5fa00167e

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.display_car()

points

over 9 years

Answer 54b3e4ad937676167f008039

class Car(object): condition = “new” def init(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg def display_car(self): return ‘this is a %s %s with %s MPG’ % (self.color, self.model, str(self.mpg))

my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

points

over 9 years

  • Free Course

    Learn SQL

    In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language. Language Fluency
  • Free Course

    Learn JavaScript

    Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity. Language Fluency

Free Course

Learn HTML

Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.

At Career Karma, our mission is to empower users to make confident decisions by providing a trustworthy and free directory of bootcamps and career resources. We believe in transparency and want to ensure that our users are aware of how we generate revenue to support our platform.

Career Karma recieves compensation from our bootcamp partners who are thoroughly vetted before being featured on our website. This commission is reinvested into growing the community to provide coaching at zero cost to their members.

It is important to note that our partnership agreements have no influence on our reviews, recommendations, or the rankings of the programs and services we feature. We remain committed to delivering objective and unbiased information to our users.

In our bootcamp directory, reviews are purely user-generated, based on the experiences and feedback shared by individuals who have attended the bootcamps. We believe that user-generated reviews offer valuable insights and diverse perspectives, helping our users make informed decisions about their educational and career journeys.

Find the right bootcamp for you

Python lỗi typeerror read_temp takes no arguments 1 given năm 2024

X

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Hi all,

Really sorry about the lack of code tags. I have them turned on in my account settings, but they're off for this post, and I can't see a button to change that. I saw a note in the forum documentation saying an administrator has to enable this. If there's a way for me to turn them on, let me know and I'll fix it. I hope my subject was descriptive enough. I am getting an error when calling a method in a class I made. The method has been defined as read_temp(self): but it still seems to think it should not take any arguments. My source code is this:

Code:

import time
import math
import pyb
from pyb import UART
class tempSensor():
    """Thermistor object takes IO pins for power and signal and                                          
    nominal resistance of parallel resistor. Methods allow for reading                                   
    the temperature. Main 'public' method will return float."""
    def __init__(self, sensor_pin, power_pin, resistance):
        """Pin args are strings corresponding to pyb names, resistance is int"""
        self.sensor_pin = pyb.ADC(sensor_pin)
        self.power_pin = pyb.Pin(power_pin, pyb.Pin.OUT_PP)
        self.resistance = resistance
        self.thermistor_nominal = 10000
        self.temperature_nominal = 25
        self.bcoefficient = 3950
        self.numsamples = 5
    def _read_sensor(self):
        """'Private' method to set power pin up and read value from thermistor.                          
        Power is only on temporarily to avoid heating up thermistor."""
        self.power_pin.high()
        time.sleep(1)
        average = 0
        for i in range(self.numsamples):
            average += self.sensor_pin.read()
        self.power_pin.low()
        return average / self.numsamples
    def read_temp(self):
        """'Public' method to allow user to get temperature value from sensor."""
        reading = self._read_sensor()
        reading = 4095 / reading - 1
        reading = resistance / reading
        temp = reading / self.thermistor_nominal
        temp = math.log(temp)
        temp /= self.bcoefficient
        temp += 1.0 / (self.temperature_nominal + 273.15)
        temp = 1.0 / temp
        temp -= 273.15
        round(temp, 1) # Accuracy will be lost beyond one floating point                                 
        return temp
def esp_transmit(uart, reading):
    uart.write("AT+CIPSEND={}\r\n".format(len(reading)))
    time.sleep(5)
    uart.write(str(reading))
def sensor_test():
    uart = UART(4, 115200)
    uart.init(115200, bits=8, parity=None, stop=1)
    uart.write('AT+CIPSTART="UDP","192.168.0.19",6789\r\n')
    time.sleep(5)
    sensor_left_power = 'Y3'
    sensor_left_read = 'Y12'
    sensor_right_power = 'Y1'
    sensor_right_read = 'Y11'
    resistance = 10000
    sensor_left = tempSensor(sensor_left_read, sensor_left_power, resistance)
    sensor_right = tempSensor(sensor_right_read, sensor_right_power, resistance)
    while True:
        esp_transmit(uart, sensor_left.read_temp())
        time.sleep(5)
        esp_transmit(uart, str(sensor_right.read_temp()))
        time.sleep(5)
sensor_test()

The last part of the code, sensor_test() and esp_transmit(), is a means for me to test the temperature reading. If I hardcode a float into the esp_transmit call and have all this sending to a UDP server on my laptop, it works fine, but if I make the call to sensor.read_temp(), I don't see anything showing up at the server end. In the REPL, when I import this source file, and create a sensor object, as soon as I call read_temp(), I get TypeError: read_temp() takes 0 positional arguments but 1 were given The same goes if I call _read_sensor() directly. This error normally occurs if self has not been used as a parameter when defining the method. That is the only case I have seen online. I did also see that MicroPython has a problem with handling arguments sometimes: It made me think that there could be some issue with how micropython is handling my code. When I define all this code in the REPL myself, however, it works without a problem. I even got it to return some values from reading the sensors. So somehow it has an issue with this module. By the way, I'm using a pyboard, V1.1. tempobjects.py, the name of this module, is in the main directory for the board. boot.py contains the instruction, pyb.main('tempobjects.py') I'm all out of ideas. Is there something I'm missing about the way MicroPython handles classes? Thanks for your help.