banana
banana

Reputation: 33

Defining a class constructor using python

Write a constructor with parameters self, num_mins and num_messages. num_mins and num_messages should have a default value of 0.

Sample output with one plan created with input: 200 300, one plan created with no input, and one plan created with input: 500

**My plan... Mins: 200 Messages: 300

Dad's plan... Mins: 0 Messages: 0

Mom's plan... Mins: 500 Messages: 0**

class PhonePlan:

    # add constructor

    def print_plan(self):
        print('Mins:', self.num_mins, end=' ')
        print('Messages:', self.num_messages)


my_plan = PhonePlan(int(input()), int(input()))
dads_plan = PhonePlan()
moms_plan = PhonePlan(int(input()))

print('My plan...', end=' ')
my_plan.print_plan()

print('Dad\'s plan...', end=' ')
dads_plan.print_plan()

print('Mom\'s plan...', end= ' ')
moms_plan.print_plan()

How would I complete this code?

Upvotes: 2

Views: 23994

Answers (5)

marcheezmo
marcheezmo

Reputation: 1

This is what worked for me.

def __init__(self, minutes=0, messages=0):
    self.num_mins = minutes
    self.num_messages = messages

Upvotes: 0

Lydia
Lydia

Reputation: 1

This is correct answer for this lab:

def __init__(self, num_mins=0, num_messages=0):
    self.num_mins = num_mins
    self.num_messages = num_messages

Upvotes: -1

Askarr
Askarr

Reputation: 21

for this specific question you are asked to make a construct with parameters 'self' 'num_mins' and 'num_messages' and equal the last two to 0.

      def __init__(self, num_mins=0, num_messages=0):
            self.num_mins = num_mins
            self.num_messages = num_messages

this sets all 3 parameters (and the two to 0 that are asked for).

Upvotes: 0

KDR87
KDR87

Reputation: 73

class PhonePlan:
   
    def __init__(self, minutes=0, messages=0):
        self.num_mins=minutes
        self.num_messages=messages

    def print_plan(self):
        print('Mins:', self.num_mins, end=' ')
        print('Messages:', self.num_messages)


my_plan = PhonePlan(int(input()), int(input()))
dads_plan = PhonePlan()
moms_plan = PhonePlan(int(input()))

print('My plan...', end=' ')
my_plan.print_plan()

print('Dad\'s plan...', end=' ')
dads_plan.print_plan()

print('Mom\'s plan...', end= ' ')
moms_plan.print_plan()

Upvotes: 3

Nishith Shetty
Nishith Shetty

Reputation: 46

You need to define a constructor in your class.

class PhonePlan:
    def __init__(self, minutes=0, messages=0):
        self.minutes = minutes
        self.messages = messages

    def print_plan(self):
        print('Mins:', self.minutes)
        print('Messages:', self.messages)


my_plan = PhonePlan(540, 10)
dads_plan = PhonePlan()
moms_plan = PhonePlan(56)

print('My plan...')
my_plan.print_plan()

print('Dad\'s plan...')
dads_plan.print_plan()

print('Mom\'s plan...')
moms_plan.print_plan()

Upvotes: 0

Related Questions