Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't seem to pass the input value to my class

class Tutu():
    def __init__(self, name, color):
        self.color = color
        self.name = name

    def introduce(self):
        print("Hey  {0} I am {1}".format(self.name, self.color) )

zizi = Tutu("Joe", "Potato")
zizi.introduce()


class Zuzu(Tutu):
    def __init__(self, potato):
        self.potato = potato

    def introduce(self):
        print("What's up {0}".format(self.potato))

x = input()
pipi = Zuzu(x)
pipi.introduce()


What I have tried:

I tried adding the input in the __init__ and in the introduce method but it doesn't work.

Error:
NameError: name 'd' is not defined
Posted
Updated 8-Mar-17 1:18am

1 solution

I do not know where that error message comes from but the code above works correctly:

  1. zizi = Tutu("Joe", "Potato") # zizi is an object of class Tutu with those attributes
  2. zizi.introduce() # display "Hey Joe I am Potato"
  3. x = input() # get input from the user, e.g. fred
  4. pipi = Zuzu(x) # pipi is an object of class Zuzu with that attribute (fred)
  5. pipi.introduce() # display "What's up fred"
 
Share this answer
 
Comments
Krokizo 8-Mar-17 7:31am    
Turns out PyCharm uses python 2.7 as interpreter instead of python 3. And it would seem that in python 2.7 it should be raw_input instead of input. Thanks for the answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900