2017-03-09 1 views
0

저는 최근에 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 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 

이것은 제작 코드이며, 암호화 및 암호 해독 소프트웨어입니다. (주석으로 표시)이 오류는, 이런 일이 왜 모르겠어요

line 23, in Encrypt 
print(self.publickey_generate(). NameError: name 'self' is not defined 

입니다. 모든 입력을 부탁드립니다.

+0

변화'수준의 암호화()'(객체) :'? – mguijarr

+7

들여 쓰기를 수정해야합니다. – Wondercricket

+0

''__init__'' 메소드에서 코드가 제대로 들여 쓰기되지 않은 것처럼 보입니다. – jakevdp

답변

1

들여 쓰기가 보일 것입니다 귀하의 초기화 방법 (publickey_generate() 제외) 모든 코드를 삽입하는 것 같은 수준의 암호화`에

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 
1

자기가 당신이 수업의 방법에있을 때 자기의 물건을 참고하고 있습니다.

여기 23 번째 줄에서 직접 수업에서 사용하고 있습니다. 제 의견으로는 좋지 않습니다. 전에 자기를 제거하면 작동합니다. publickey_generate()

솔직히 오브젝트 프로그래밍을위한 이상한 방법 인 것처럼 보입니다 ... 모든 코드를 클래스 메소드에 넣고 필요한 경우 전역 변수 만 넣어야합니다. 따라서 귀하의 경우, 가장 쉬운

관련 문제