Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误--”;“自我”;没有定义_Python_Self - Fatal编程技术网

Python错误--”;“自我”;没有定义

Python错误--”;“自我”;没有定义,python,self,Python,Self,我最近开始学习Python,遇到了一个错误,没有定义类和“self”。我知道有很多关于这个问题的问题,但是基于这些答案,我无法用我的代码找出问题所在 import random class Encrypt(): def __init__(self, master): self.master = master master.title("Encryption using RSA Algorithms") x = input("Do you wan

我最近开始学习Python,遇到了一个错误,没有定义类和“self”。我知道有很多关于这个问题的问题,但是基于这些答案,我无法用我的代码找出问题所在

import random

class Encrypt():
    def __init__(self, master):
        self.master = master
        master.title("Encryption using RSA Algorithms")

    x = input("Do you want to Encrypt or Decrypt?")
    if x=="Encrypt":
        print("Redirecting...")
    else:
        print("Redirecting...")

    choice_1 = "Encrypt"
    choice_2 = "Decrypt"

    if x==choice_1:
        y  = input("Do you have a public key?")
        if y=="Yes":
            print("Redirecting...")
        else:
            print("Generating a key...")
            print(self.publickey_generate()) #Error here that self is not defined

    else:
        z = input("What is your private key?")

    def publickey_generate(self):
        for output in range (0,1000):
            public = random.randint(0, 1000)
            if public <= 500:
                public += public
            else:
                public = public - 1

我不知道为什么会这样。欢迎您的任何意见

self在类的方法中引用对象的实例

在这里,在第23行中,您直接在课堂上使用它(在我看来,这不是一个好方法)。如果在
publickey\u generate()之前删除self,它应该可以工作


但老实说,对于对象编程来说,这似乎是一种非常奇怪的方式。。。您应该将所有代码放在类方法中,并且只将全局变量放在外部(如果需要)。因此,在您的情况下,最简单的方法是将所有代码(除了
publickey_generate()
)放在init方法中

当您在类的方法中时,self正在引用对象的实例

在这里,在第23行中,您直接在课堂上使用它(在我看来,这不是一个好方法)。如果在
publickey\u generate()之前删除self,它应该可以工作


但老实说,对于对象编程来说,这似乎是一种非常奇怪的方式。。。您应该将所有代码放在类方法中,并且只将全局变量放在外部(如果需要)。因此,在您的情况下,最简单的方法是将所有代码(除了
publickey_generate()
)放入init方法中

缩进应该如下所示:

import random

class Encrypt():
    def __init__(self, master):
        self.master = master
        master.title("Encryption using RSA Algorithms")

        x = input("Do you want to Encrypt or Decrypt?")
        if x=="Encrypt":
            print("Redirecting...")
        else:
            print("Redirecting...")

        choice_1 = "Encrypt"
        choice_2 = "Decrypt"

        if x==choice_1:
            y  = input("Do you have a public key?")
        if y=="Yes":
            print("Redirecting...")
        else:
            print("Generating a key...")
            print(self.publickey_generate()) #Error here that self is not defined

        else:
            z = input("What is your private key?")

    def publickey_generate(self):
        for output in range (0,1000):
            public = random.randint(0, 1000)
            if public <= 500:
                public += public
            else:
                    public = public - 1
随机导入
类Encrypt():
定义初始(自我,主):
self.master=master
主标题(“使用RSA算法的加密”)
x=输入(“您想加密还是解密?”)
如果x==“加密”:
打印(“重定向…”)
其他:
打印(“重定向…”)
choice_1=“加密”
choice_2=“解密”
如果x==选项_1:
y=输入(“您有公钥吗?”)
如果y==“是”:
打印(“重定向…”)
其他:
打印(“生成密钥…”)
打印(self.publickey_generate())#此处错误,未定义self
其他:
z=输入(“您的私钥是什么?”)
def公钥_生成(自身):
对于范围(01000)内的输出:
public=random.randint(0,1000)

如果公共缩进应如下所示:

import random

class Encrypt():
    def __init__(self, master):
        self.master = master
        master.title("Encryption using RSA Algorithms")

        x = input("Do you want to Encrypt or Decrypt?")
        if x=="Encrypt":
            print("Redirecting...")
        else:
            print("Redirecting...")

        choice_1 = "Encrypt"
        choice_2 = "Decrypt"

        if x==choice_1:
            y  = input("Do you have a public key?")
        if y=="Yes":
            print("Redirecting...")
        else:
            print("Generating a key...")
            print(self.publickey_generate()) #Error here that self is not defined

        else:
            z = input("What is your private key?")

    def publickey_generate(self):
        for output in range (0,1000):
            public = random.randint(0, 1000)
            if public <= 500:
                public += public
            else:
                    public = public - 1
随机导入
类Encrypt():
定义初始(自我,主):
self.master=master
主标题(“使用RSA算法的加密”)
x=输入(“您想加密还是解密?”)
如果x==“加密”:
打印(“重定向…”)
其他:
打印(“重定向…”)
choice_1=“加密”
choice_2=“解密”
如果x==选项_1:
y=输入(“您有公钥吗?”)
如果y==“是”:
打印(“重定向…”)
其他:
打印(“生成密钥…”)
打印(self.publickey_generate())#此处错误,未定义self
其他:
z=输入(“您的私钥是什么?”)
def公钥_生成(自身):
对于范围(01000)内的输出:
public=random.randint(0,1000)

如果将
class Encrypt()
更改为
class Encrypt(对象):
?您的缩进需要更正编辑看起来代码在
\uuuuu init\uuuuuuuuuu
方法下没有正确缩进将
class Encrypt()
更改为
class Encrypt(对象):
?您的缩进需要更正编辑看起来代码在
\uuuu init\uuuu
方法下没有正确缩进