Python Forum
what's wrong with my code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: what's wrong with my code (/thread-22191.html)



what's wrong with my code - Oaklizard2047 - Nov-03-2019

So i was reading the Python Tutorial and decided to follow one of it's code :
class Cat:
	def __init__(self, name):
		self.name = name
		self.tricks = []

	def add_trick(self, trick):
		self.tricks.append(trick)
then i put this in the console :
a = Cat('Kelly')
a.add_trick('sit')

but this is what i got:
Error:
File "<stdin>", line 1, in <module> File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\cat.py", line 7, in add_trick self.tricks.append(trick)
can anyone tell why did this happen? If there's a mistake in the code, please tell the solution


RE: what's wrong with my code - Evil_Patrick - Nov-03-2019

This working Fine for me


class Cat:
    def __init__(self, name):
        self.name = name
        self.tricks = []
 
    def add_trick(self, trick):
        self.tricks.append(trick)

a = Cat('Kelly')
a.add_trick('sit')

print(a.name)
print(a.tricks)
Output:
Kelly ['sit']



RE: what's wrong with my code - Oaklizard2047 - Nov-03-2019

i forgot a line in the error message:
Error:
File "<stdin>", line 1, in <module> File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\cat.py", line 7, in add_trick self.tricks.append(trick) AttributeError: 'Cat' object has no attribute 'trick'



RE: what's wrong with my code - Evil_Patrick - Nov-03-2019

(Nov-03-2019, 05:37 AM)Oaklizard2047 Wrote: i forgot a line in the error message:
Error:
File "<stdin>", line 1, in <module> File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\cat.py", line 7, in add_trick self.tricks.append(trick) AttributeError: 'Cat' object has no attribute 'trick'

Can you Post a Screenshot of code window and output?


RE: what's wrong with my code - Oaklizard2047 - Nov-03-2019

(Nov-03-2019, 05:45 AM)Evil_Patrick Wrote:
(Nov-03-2019, 05:37 AM)Oaklizard2047 Wrote: i forgot a line in the error message:
Error:
File "<stdin>", line 1, in <module> File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\cat.py", line 7, in add_trick self.tricks.append(trick) AttributeError: 'Cat' object has no attribute 'trick'
Can you Post a Screenshot of code window and output?
for some reason it Works now. I guess it's just a bug... Thanks for your help though