- Joined
- May 11, 2024
- Messages
- 16
- Reaction score
- 0
- Points
- 6
Python:
class phm:
def __init__(self, name, company):
self.name = name
self.company = company
def show (self):
print ("hello my name is: " + self.name + " and I'm working at: " + self.company + ".")
obj = phm ("John","Phantasma")
obj.show()
Explanation:
In this example, we are creating a phm class and we have created the name, and company instance variables in the constructor. We have created a method named show() which returns the string “Hello my name is ” + {name} +” and I work in “+{company}+”.”.We have created a person class object and we passing the name John and Company Phantasma to the instance variable. Finally, we are calling the show() of the class.
Python:
class phm:
def __init__(self, name, company):
self.name = name
self.company = company
def show (self):
print ("hello my name is: " + self.name + " and I'm working at: " + self.company + ".")
name = (input("Enter your name: "))
company = (input("Enter your company: "))
print("\n")
obj = phm (name, company)
obj.show()
Explanation:
I used the same manner, but the difference is that I used the input() function. This set of code requests user input and stores it in the message variable, which contains the name and company.