Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what's wrong with my code
#1
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
Reply
#2
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']
Reply
#3
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'
Reply
#4
(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?
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 849 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,555 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,249 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,781 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,537 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,526 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,630 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,497 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,018 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020