Jump to content

I'm Learning python and I decided to try to implement something I learned to make a very small program Just to see if it would work. Of course, it doesn't and before anyone says anything, I know there is an easier way to do this but I just wanted to see if I could do this a different way.

 

The program basically just asks the user what their name is and repeats it back to them. Seems pretty simple but I have run into a problem for some odd reason.

 

Here is the code:

 

class Name(object):

def __init__(self, lyrics):
self.mane = name
 
def Hi(self):
for line in self.name:
print line
 
print "What is your name?"
name = raw_input(" >")
 
user = name
 
print "Your name is: %r" % user.Hi
 
Here is the error:
Traceback (most recent call last):

  File "TesticularCancer.py", line 14, in <module>

    print "Your name is: %r" % user.Hi

AttributeError: 'str' object has no attribute 'Hi'

Link to comment
https://linustechtips.com/topic/330583-python-class-halp/
Share on other sites

Link to post
Share on other sites

Dat filename! 

 

 

In this line you're just adding input to the variable name. So name is a string, not the class you've defined. And therefore user will also be a string.

name = raw_input(" >")user = name

What you want is this which creates a Name class object, passing the name string into it, and assigning that object to user.

name = raw_input(" >")user = Name(name)
Link to comment
https://linustechtips.com/topic/330583-python-class-halp/#findComment-4488290
Share on other sites

Link to post
Share on other sites

I'm Learning python and I decided to try to implement something I learned to make a very small program Just to see if it would work. Of course, it doesn't and before anyone says anything, I know there is an easier way to do this but I just wanted to see if I could do this a different way.

 

The program basically just asks the user what their name is and repeats it back to them. Seems pretty simple but I have run into a problem for some odd reason.

 

Here is the code:

 

class Name(object):

def __init__(self, lyrics):
self.mane = name
 
def Hi(self):
for line in self.name:
print line
 
print "What is your name?"
name = raw_input(" >")
 
user = name
 
print "Your name is: %r" % user.Hi
 
Here is the error:
Traceback (most recent call last):

  File "TesticularCancer.py", line 14, in <module>

    print "Your name is: %r" % user.Hi

AttributeError: 'str' object has no attribute 'Hi'

 

 

Firstly, you have a typo when you say 

self.mame = name

instead of

self.name = name

You error is caused you are trying to access the Hi attribute(basically a variable in a class) how the string class has no such attribute. Instead you want to call the Hi function to do this you need to create an object of name. You need to change your code to:

class Name(object):   def __init__(self, lyrics):       self.name = name    def Hi(self):       for line in self.name:           print line print "What is your name?"name = raw_input(" >") user = Name(name) print "Your name is: %r" % user.Hi()
Link to comment
https://linustechtips.com/topic/330583-python-class-halp/#findComment-4488308
Share on other sites

Link to post
Share on other sites

 

Dat filename! 

 

 

In this line you're just adding input to the variable name. So name is a string, not the class you've defined. And therefore user will also be a string.

name = raw_input(" >")user = name

I believe what you want is this which creates a Name class object, passing the name string into it, and assigning that object to user.

name = raw_input(" >")user = Name(name)

Thank you so much and  I choose that name because every other variation of "Test" was already in use on my computer  :D

Link to comment
https://linustechtips.com/topic/330583-python-class-halp/#findComment-4488340
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×