类中的函数不接受参数的问题

问题描述 投票:0回答:1

我创建了一个类,并为该类创建了函数供我稍后使用(函数有参数),但我遇到了一个问题,我的函数之一不接受参数。我认为这可能是因为它嵌套在类中的另一个函数中(两者都是在类中定义的),但我对编程还很陌生,所以我很困惑。

class Encoder():
    def __init__(self):
        self.chars = " " + string.punctuation + string.digits + string.ascii_letters
        self.chars = list(self.chars)
        self.key = self.chars.copy()
        random.shuffle(self.key)
    
    def create_files(self):
        with open("key.txt", 'a') as key_file:
                key_file.write(str(self.key))  

        with open("encrypted.txt", 'a') as encrypted_file:
            encrypted_file.write("new file")
        
    def file_to_chars(Encoder, file_name):
        with open(file_name, 'r') as user_file:
                words = user_file.readlines()
                chars_list = []
                for word in words:
                    for char in list(word):
                        chars_list.append(char)
                while "\n" in chars_list:
                    chars_list.remove("\n")
                    
        return chars_list            


    def encode(self, user_input):
        original = self.file_to_chars(Encoder, user_input)
        encryption = ""
        for letter in original:
            index = self.chars.index(letter)
            encryption += self.key[index]
        
        with open("encrypted.txt", 'w') as encrypted_file:
            encrypted_file.write(str(encryption))
        try:
            return open("encrypted.txt", 'r')
        except:
            print("Error, try again. ")

当我尝试在encode()函数中传递“user_input”的参数时,我收到一条错误,指出files_to_chars()没有file_name的参数。

from encoder import Encoder

ui = input('name of file: ')
f=Encoder.encode(Encoder, ui)
print(f.read())

这是一个如何使用它的示例。

我尝试将

Encoder,
作为 files_to_chars 函数的第一个参数,但这似乎不起作用,我只是得到了同样的错误。我很迷失,任何帮助都会很棒。

python function file class parameters
1个回答
0
投票
class Encoder:
def __init__(self):
    self.chars = " " + string.punctuation + string.digits + string.ascii_letters
    self.chars = list(self.chars)
    self.key = self.chars.copy()
    random.shuffle(self.key)

def create_files(self):
    with open("key.txt", "a") as key_file:
        key_file.write(str(self.key))

    with open("encrypted.txt", "a") as encrypted_file:
        encrypted_file.write("new file")

def file_to_chars(self, file_name): # Change Encoder to self
    with open(file_name, "r") as user_file:
        words = user_file.readlines()
        chars_list = []
        for word in words:
            for char in list(word):
                chars_list.append(char)
        while "\n" in chars_list:
            chars_list.remove("\n")

    return chars_list

def encode(self, user_input):
    original = self.file_to_chars(user_input) # Not need to pass Encoder
    encryption = ""
    for letter in original:
        index = self.chars.index(letter)
        encryption += self.key[index]

    with open("encrypted.txt", "w") as encrypted_file:
        encrypted_file.write(str(encryption))
    try:
        return open("encrypted.txt", "r")
    except:
        print("Error, try again. ")

我已经做了一些改变。

© www.soinside.com 2019 - 2024. All rights reserved.