我正在嘗試使用類和函數向字符串中每兩個字母添加一個隨機字母class Encryption(): def __init__(self,seed): # Set a random seed and a self.seed attribute self.seed = seed # Create an empty string attribute to hold the encrypted phrase self.encrypted_phrase = '' # Use the string and random libraries to create two attributes # One is the correct alphabet, another is a shuffled alphabet: self.correct_alphabet = list(string.ascii_lowercase) self.shuffeled_alphabet = random.sample(correct_alphabet, seed) def encryption(self,message): appended_message = list(message) for let in list(range(0, len(message), 2)): if let in self.correct_alphabet: appended_message.append(self.shuffeled_alphabet) return appended_message所以如果我這樣做e2 = Encryption(3)e2.encryption(hello)它失敗并顯示以下消息NameError Traceback (most recent call last)<ipython-input-24-505d8a880fb2> in <module>----> 1 e2.encryption(message=hello)NameError: name 'hello' is not defined我究竟做錯了什么?
2 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
hello 需要是一個字符串,因為它不是一個變量。
嘗試e2.encryption("hello")或類似的東西。
所以你的完整代碼示例是:
e2 = Encryption(3)
e2.encryption("hello")
添加回答
舉報
0/150
提交
取消