r/learnprogramming • u/Dunno978 • 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()
1
u/AdministrativeFile78 2d ago
did u run the code
1
u/specialpatrol 2d ago
The second version of the code never passed the input to the constructor
1
u/AssistantNo3545 2d ago
Your instinct is right.
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:
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
which is what I was expecting.