加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python / PyCharm缺少参数’self’?

发布时间:2020-12-20 11:08:16 所属栏目:Python 来源:网络整理
导读:我目前正在学习 Python,我不明白我的代码有什么问题,但PyCharm一直给我以下错误: Traceback (most recent call last):File "C:/Users/Sam/PycharmProjects/untitled1/app.py",line 5,in module fish1.bubbles()TypeError: bubbles() missing 1 required pos
我目前正在学习 Python,我不明白我的代码有什么问题,但PyCharm一直给我以下错误:

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'

这是我的代码:

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))

这是我的Fish.py文件:

class Fish:

    def __init__(self,name,coords):
        self.name = name
        self.coords = coords

    def bubbles(self):
        print("{0} blew some bubbles".format(self))

任何帮助,将不胜感激!

解决方法

您在 Creating Instance Objects中有错误

在本节中,fish1 = Fish您没有创建实例,但尝试使用fish1.bubbles().

所以尝试改变fish1 = Fish to fish1 = Fish(name,coords).

构造函数中需要的名称和坐标.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读