r/learnprogramming • • 2d ago

Solved Super simple question from a beginner beginner

I am following the coding tasks from an old post from here:
https://www.reddit.com/r/learnprogramming/comments/9c380n/heres_a_list_of_coding_exercises_to_help_get_you/?sort=new

and am at the 3rd and 4th Step. I assume I understood what is asked of me but my solution feels a bit too simple? And I am thinking I just found a solution but not necessarily for the question asked. Could someone double check if my code actually does what the task asks of me?

3.
Create a class called "Car" that has 1 variable called "licensePlate". Within the class, create a default constructor that sets "licensePlate" to 'Unknown' and a method/function called "getLicensePlate" that returns the variable "licensePlate".

Code:
class car:

license_plate = "Unknown"

p1=car()

def getlicense_plate():

print(p1.license_plate)

getlicense_plate()

4. Add a second Car constructor to our class that takes 1 input of type String. This is the license plate number. Assign the value of the inputLicensePlate to our variable licensePlate.

Code:
class car:

license_plate = input()

p1=car()

def getlicense_plate():

print(p1.license_plate)

getlicense_plate()

4 Upvotes

12 comments sorted by

1

u/AdministrativeFile78 2d ago

did u run the code

0

u/Dunno978 2d ago

yup and if gives out what is expected of the task, But if the goal was the output of "unknown" I could have written print("unkown") I just wanted to double check if I understood the task correctly and found the solution anticipated ^^"

2

u/AdministrativeFile78 2d ago

a constructor in python is a method called `__init__ ` so something like `def __init__(self): self.license_plate = "Unknown" ` and its asking u to return a value, not print text to the screen. `return self.license_plate`

1

u/specialpatrol 2d ago

The second version of the code never passed the input to the constructor

1

u/Dunno978 2d ago

Didn't receive any error, it just asked for an input and printed out exactly what I put in.

1

u/lanternkettle 2d ago

Python accepts it because `input()` runs when the class is defined, a constructor would receive the value through `__init__`

1

u/AssistantNo3545 2d ago

Your instinct is right.

1

u/Dunno978 2d ago

Welp okay ._. now to figure out what went wrong

1

u/LukaRozman27 2d ago

You need to use __init__ as the constructor and set self.licensePlate inside it, not as a class variable. Method should be def getLicensePlate(self): return self.licensePlate, then call p1.getLicensePlate().

1

u/mkhanwrites_ 1d ago

There's a lot wrong with this code. First off you have to learn how to run your code to check if it runs (though that doesn't necessarily mean the output will be correct - you will have to verify through testing) or results in an error.

Refer to this article. It is extremely resourceful - giving links to various components of programming you might want to read about as a beginner beginner. Including how to install Python, IDEs, text editors, books, code samples, and more: https://www.python.org/about/gettingstarted/

I did go ahead and create a working code sample that solves the questions you posted. Here it is:

# 3. First Class
class Car1:
    def __init__(self):
        self.licensePlate = 'Unknown'
    
    def getLicensePlate(self):
        return self.licensePlate


# 4. Second Class
class Car2:
    def __init__(self, inputString):
        self.licensePlate = inputString 
    
    def getLicensePlate(self):
        return self.licensePlate


def test_Car1():
    car1 = Car1()
    print(car1.getLicensePlate()) # Expect output: 'Unknown'


def test_Car2():
    car2 = Car2('Hello, World')
    print(car2.getLicensePlate()) # Expect output: 'Hello, World'


if __name__ == "__main__":
    test_Car1()
    test_Car2()

I'm not going to explain too much but as you can see there are patterns with creating Python classes with getter methods. Note the use of 'self' in constructing a class. I'm not going to explain why (honestly it's been a while for so I've sort of forgotten - might have to do with the topic of 'binding'). Instance variables must be referred to using 'self' for the namespace. A good rule of thumb when coding is to test your code (Test-Driven Development (TDD)), so I created a couple test functions to help me verify the correctness of my code. After running the test functions in the main method (the code block starting with if __name__ == "__main__":), I observed the output

Unknown
Hello, World

which is what I was expecting.