I'm currently learning Python and I don't understand what is wrong with my code, but PyCharm keeps giving me the following error:
Traceback (most recent call last):
File "C:/Users/Sam/PycharmProjects/untitled1/app.py", line 5, in <module>
fish1.bubbles()
TypeError: bubbles() missing 1 required positional argument: 'self'
Here is my code:
import random
from Fish import Fish
fish1 = Fish
fish1.bubbles()
fish1.name = input("enter the name of your fish: ")
fish1.coords = ("({0},{1})".format(random.randint(1, 100), random.randint(1, 100)))
print("The fish's name is {0}, and it is swimming at co-ordinates{1}".format(fish1.name, fish1.coords))
and here is my Fish.py file:
class Fish:
def __init__(self, name, coords):
self.name = name
self.coords = coords
def bubbles(self):
print("{0} blew some bubbles".format(self))
Any help would be appreciated!
You have the error in Creating Instance Objects
In this section fish1 = Fish you didn't create the instance, but try to use it fish1.bubbles().
So try change fish1 = Fish to fish1 = Fish(name, coords).
name and coords required in your constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With