Problems with Python OOP - python

I decided to study python's OOP on my own. In the "car" class, I create the "carHP" function, then I want to write the result of this function to the "power" variable, but the compiler throws an error. What could be the problem? I wrote it without class, everything works, but then the meaning of OOP is lost
class car():
nameCar=""
def carHP(self):
if self.nameCar=="BMW":
HP=666
return HP
power=carHP()
def infoCar(self):
print("You owner: ", self.nameCar)
print("HP car: ", self.power )
class person():
namePerson=""
def infoPerson(self):
print("Ваше имя: ",self.namePerson)
person1=person()
person1.namePerson=input("Enter your name: ")
car1=car()
car1.nameCar=input("Enter car name: ")
person1.infoPerson()
car1.infoCar()

you need to know the basics of python oop because you are using class variable instead of instance variables, do your research about the difference of both and simplify the logic
here is a better way for you to start
class Car:
def __init__(self, car_name, car_hp=100):
self.car_name = car_name
self.car_hp = car_hp
class Person:
def __init__(self, person_name):
self.person_name = person_name
def person_info(self):
print(f"Person Name: {self.person_name}")

I found some things to work on my end with a few tweaks on your code. Check them out...
I noticed how you have not called the function carHP() which may be important at initializing HP car: to 666
person1=person()
person1.namePerson=input("Enter your name: ")
car1=car()
car1.nameCar=input("Enter car name: ")
person1.infoPerson()
car1.infoCar()
Secondly, inheritance inside a class method do not receive instances automatically. They require the self. parameter to receive the contents of the method it came from. otherwise, you are returning an instance of that variable but without its parent method
I apologies if that does not make sense. But please have a look at https://docs.python.org/3/tutorial/classes.html for more information.
With the self parameter added and function called to be a conditional statement we get the following output...
class car:
nameCar = ""
power = None
def carHP(self):
if self.nameCar == "BMW":
HP = 666
self.power = HP # notice this change
def infoCar(self):
print("You owner: ", self.nameCar)
print("HP car: ", self.power)
class person():
namePerson = ""
def infoPerson(self):
print("Ваше имя: ", self.namePerson)
person1 = person()
person1.namePerson = input(str("Enter your name: "))
car1 = car()
car1.nameCar = input(str("Enter car name: "))
car1.carHP() # notice this change
person1.infoPerson()
car1.infoCar()
Output:
Enter your name: Name
Enter car name: BMW
Ваше имя: Name
You owner: BMW
HP car: 666
Process finished with exit code 0

Related

I need to figure out how to save the user input as the new variables, not sure what to do from here

#I need to figure out how to save the user input as the new variables, not sure what to do from here.
class Person:
def __init__(self):
#I'm supposed to have the object set to these generic names at first and then after the prompt function they need to be updated.
self.name = 'anonymous'
self.b_year = 'unknown'
def prompt(self):
print('Please enter the following:')
self.name = input('Name: ')
self.b_year = input('Year: ')
b = Book()
b.prompt()
def display(self):
print(f'Author:\n{self.name} (b. {self.b_year})\n')
class Book:
def __init__(self):
self.title = 'untitled'
self.publisher = 'unpublished'
def prompt(self):
self.title = input('Title: ')
self.publisher = input('Publisher: ')
def display(self):
print(f'\n{self.title}\nPublisher:\n{self.publisher}')
p = Person()
`enter code here`p.display()
def main():
p = Person()
b = Book()
b.display()
p.prompt()
b.display() #right here I need it to display the new information
if __name__ == '__main__':
main()
Two things you need to do here. First of all you need to initialize your variables in your Person class. Second of all I don't understand what the point of creating objects in a class and then doing the same outside of the class. I suggest taking the following code and modifying it to your likings. Additionally I reorganized the way you call your methods. First you prompt the user with all the necessary questions, then display them all together.
class Person:
def __init__(self):
self.name = None
self.b_year = None
def prompt(self):
print('Please enter the following:')
self.name = input('Name: ')
self.b_year = input('Year: ')
def display(self):
print(f'Author:\n{self.name} (b. {self.b_year})\n')
class Book:
def __init__(self):
self.title = 'untitled'
self.publisher = 'unpublished'
def prompt(self):
self.title = input('Title: ')
self.publisher = input('Publisher: ')
def display(self):
print(f'\n{self.title}\nPublisher:\n{self.publisher}')
def main():
p = Person()
b = Book()
b.display()
p.display()
p.prompt()
b.prompt()
b.display()
p.display()#right here I need it to display the new information
if __name__ == '__main__':
main()
output
untitled
Publisher:
unpublished
Author:
None (b. None)
Please enter the following:
Name: a
Year: b
Title: c
Publisher: d
c
Publisher:
d
Author:
a (b. b)
I'm not designer so you don't have to go with my idea, but IMO, don't add \n like this '\n{self.title}\nPublisher:\n{self.publisher}'. It looks a bit weird in the output.
You create one Book in the main function, and then another Book inside the Person.prompt method. They aren't the same Book - you have two!
You need to decide if a book belongs within a person, or whether they are two top level objects that may have a relationship. From a "real world" perspective, I don't think a person should automatically have a book associated with them. Perhaps you could create a Book and then pass to the Person constructor...

Python error: object has no attribute

I'm new to coding -- taking a Python college course. I know this will be obvious to many of you, but I can not figure out why I continue to get this error attribute error:
prodworker = employee.ProductionWorker(shift_num, pay_rate)
AttributeError: 'Employee' object has no attribute 'ProductionWorker'
Any help is greatly appreciated :)
class Employee: #writing new class called Employee:
def __init__(self, name, number): #accepts arguments for employee name and
self.__name = name #employee number
self.__number = number
def set_name(self, name): #mutator methods to set name and number
self.__name = name
def set_number(self, number):
self.__number = number
#accessor methods returns name and number
def get_name(self):
return self.__name
def get_number(self):
return self.__number
class ProductionWorker(Employee): #write subclass
def __init__(self, shift_num, pay_rate):
Employee.__init__(self, 'ProductionWorker')
self.__shift_num = shift_num
self.__pay_rate = pay_rate
def set_shift_num(self, shift_num):
self.__shift_num = shift_num
def set_pay_rate(self, pay_rate):
self.__pay_rate = pay_rate
def get_shift_num(self):
return self.__shift_num
def get_pay_rate(self):
return self.__pay_rate
#This program creates an instance of Employee Class
#and an instance of Production Worker Class:
again = 'Y'
while again.upper() == 'Y':
print('Enter the following data for the employee: \n')
name = input('What is the employee name?: ')
number = input('What is the employee number? ')
shift_num = input('What is the employee shift number? 1 = Day, 2 = Night :')
while shift_num != '1' and shift_num != '2':
shift_num = input('Invalid entry! Enter 1 for Day shift or 2 for Night shift!')
else:
if shift_num == '1':
shift_num = 'Day'
if shift_num == '2':
shift_num = 'Night'
pay_rate = float(input('What is the employee pay rate? '))
print()
print('This is an instance of the Employee class:')
employee = Employee(name, number)
print('EMPLOYEE: \t\t'+ employee.get_name())
print('EMPLOYEE NUMBER: \t' + employee.get_number())
print()
print('This is an instance of the Production Worker class: ')
prodworker = employee.ProductionWorker(shift_num, pay_rate)
print('SHIFT: \t\t\t' + ProductionWorker.get_shift_num())
print('PAY RATE: \t\t$' + format(ProductionWorker.get_pay_rate(), ',.2f'), sep='')
print('--------------------------------------------')
again = input('Enter Y to add another: ')
if again.upper() != 'Y':
print('Program End')
The ProductionWorker class is a subclass of the Employee class, but that doesn't mean you can call it through an instance of Employee. It's still a top-level class that you should call directly. Try replacing employee.ProductionWorker(...) with just ProductionWorker(...).
You'll get past the current error, but you may have new ones. For instance, I think the current attempt to call Employee.__init__ from ProductionWorker.__init__ will fail because it doesn't pass the right number of arguments. You may also have logic issues, if you expected employee.ProductionWorker to create a ProductionWorker instance that was related in some way to the employee object.
I'd also discourage you from using __double_leading_underscore names for your attributes. That invokes Python's name mangling system, which is mostly intended to help prevent accidental reuse of the same name from different classes in a large or unpredictable inheritance hierarchy. If you just want your attributes to be "private", use a single underscore. That doesn't protect them from being accessed by outside code, but it serves as documentation that they're not part of the object's public API. One of Python's design philosophies is that it's programmers are responsible for their own behavior (often described with the phrase "We're all consenting adults"). If a programmer wants to access an object's private data they can do so (which can be very useful for debugging). But if they break stuff, they have no one to blame but themselves.

Python Class and encapsulation methods

I'm new to OOP and this is my first shot at creating a Python class. I am trying to make my 3 variables private and so that only the methods update the info (enforce encapsulation). It seems that if I remove the setters and getters methods from my class, it has no impact on my code (must be the initializer method doing the work?). What can I do to improve this? Thanks.
Edit- i've updated my code and removed the init. My getters are not working now.
#Instantiate a new Pet Instance.
myPet = Pet()
#Get input from user.
myPet.setName = input("Enter the pet's name: ")
myPet.setTypes = input("Enter the pet's type (Dog, Cat, Bird, etc.): ")
myPet.setAge = input("Enter the pet's age (in years): ")
while myPet.setAge.isalpha():
print()
print("Age cannot contain numbers. Try again.")
myPet.setAge = input("Enter the pet's age (in years): ")
#Call the showPetInfo module using new instanced object.
myPet.showPetInfo()
class Pet:
#Fields of the Pet Class.
__PetName = ""
__PetType = ""
__PetAge = ""
#Setter methods.
def setName(self,name):
self.__PetName = name
def setTypes(self,types):
self.__PetType = types
def setAge(self,age):
self.__PetAge = age
#Getter methods.
#property
def getName(self):
return self.__PetName
#property
def getType(self):
return self.__PetType
#property
def getAge(self):
return self.__PetAge
def showPetInfo(self):
print("\n \n \n \n")
print("Here is your pet's information. Your pet's name is {}, it is a {} and it is {} years old.".format(self.getName,self.getType,self.getAge))
main()
you are unfortunately right, they use to say setters/getters are contracts doing restriction for adults... (if I tell you "dont touch it" then you shoulntd touch it) but there is nothing restricting you and you can modify them!
same "feature" can be observed with "constants"... do in the jupyther or the terminal this
import math
math.pi = 1
a = math.pi
a
math.pi
and you will see that you now modified the constant pi value to 1
many sugest to usse properties but that is not a capsulation at all, that is just sugar syntax for the same "adults contract" IMHO :)
so to your question
What can I do to improve this?
document the code you are writing so the other part using it is aware about how the code, instances states in objects must be handled

Class isn't outputting arguments/doing anything? (Python)

class Fridge:
def __init__ (self, food, quantity):
self.food=food
self.quantity=quantity
def UserEntry(self):
if input=="milk":
print("you got milk!")
else:
print ("What do you want?")
def DisplayFridge(self):
print("Fridge_item#1 :" , self.food, "Quantity:" , self.quantity)
When I attempt to instantiate the class, such as:
test= Fridge
and as soon as a I open the parenthesis in order to instantiate the class such as follows:
test = Fridge (
I am presented with the arguments that were passed to the class constructor/initialization method. (i.e. food and quantity).
With that in mind then....I am at a bit of a loss as to why I am not getting any output. nor, am I being asked for input, etc.
You are not getting any input this way, you should try :
class Fridge:
def __init__ (self, food, quantity):
self.food=food
self.quantity=quantity
def UserEntry(self):
var = raw_input("Please enter something: ")
if var=="milk":
print("you got milk!")
else:
print ("What do you want?")
def DisplayFridge(self):
print("Fridge_item#1 :" , self.food, "Quantity:" , self.quantity)
But there is serious lack of logic in your code :
Why UserEntry is never used ?
How do you use Fridge ?
You userEntry method will never change your self.food variable.
If you're making an instance, you type
test = Fridge(
And then it doesn't show you "the arguments that were passed to the class constructor/initialization method", but it shows you what you have to pass in order to make an instance.
E.g.
test = Fridge("milk", 10)
And now it holds 10 milks. Try
test.UserEntry()
test.DisplayFridge()

Homework Help - Object-Oriented Programming

In my intro class, we just started the section on object-oriented programming. This class is the first I've ever been exposed to programming, and I'm really not understanding it.
We have an assignment where we have to create an Animal class, a Zoo class, and then a zookeeper program to run the information from the first two classes. I have the programs typed up based off of examples in my book, but am still not doing it correctly.
If you could look over my codes and give me some feedback or help, that would be greatly appreciated!
class Animal:
def __innit__(self, animal_type, name):
self.animal_type = animal_type
self.name = name
def get_animal_type(self, animal_type):
self.__animal_type = animal_type
def get_name(self, name):
self.__name = name
def check_mood(self, mood):
input random.txt
print random.random()
Class Zoo:
def __innit__(self):
self.__animals = animal_list
def add_animals(self, animal):
self.__animals.append(animal)
def show_animals(animal_list):
return animal_list
input Animal.py
input Zoo.py
def main():
ADD_ANIMAL = 1
SHOW_ANIMALS = 2
EXIT = 3
def get_manu_choice():
print()
print("Zoo Options")
print("-----------")
print("1. Add Animal")
print("2. Show Animals")
print("3. Exit")
print()
choice = int(input("What would you like to do? "))
while choice < ADD_ANIMAL or choice > EXIT:
choice = int(input("Please choose a valid option: "))
return choice
main()
innit should be init
Looks to me like you are missing a chunk of functionality. You need an instance of zoo, and then in response to the input, either add another animal to the zoo or print the list of animals.

Categories