为了账号安全,请及时绑定邮箱和手机立即绑定

如何用随机字母添加每隔一个字母?

如何用随机字母添加每隔一个字母?

达令说 2021-10-05 16:16:24
我正在尝试使用类和函数向字符串中每两个字母添加一个随机字母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条经验 获得超5个赞

hello 需要是一个字符串,因为它不是一个变量。


尝试e2.encryption("hello")或类似的东西。


所以你的完整代码示例是:


e2 = Encryption(3)

e2.encryption("hello")


查看完整回答
反对 回复 2021-10-05
  • 2 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号