Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
class Flower: def __init__(self, name='', num_petals=0, price=0): self.name = name self.num_petals = num_petals self.price = price def get_name(self): return self.name def get_num_petals(self): return self.num_petals def get_price(self): return self.price def set_name(self, name): self.name = name def set_num_petals(self, num_petals): self.num_petals = num_petals def set_price(self, price): self.price = price flower = Flower() flower.set_name('Rose') flower.set_price(4.5) flower.set_num_petals(15) print("Name:", flower.get_name()) print("Price:", flower.get_price()) print("Number of petals:", flower.get_num_petals())
Get Answers For Free
Most questions answered within 1 hours.