首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基本Python Vigenere Cypher

基本Python Vigenere Cypher
EN

Stack Overflow用户
提问于 2018-10-27 06:33:50
回答 1查看 419关注 0票数 0

我有基本的代码布局,问题是我不知道如何正确使用关键字的长度。

当我运行这个问题时,我只是得到了一堆S作为我的输出,因为代码不会到达关键字的下一个字母。

我需要帮助在定义加密部分(第二个定义)

Keyword is SECRET //这是输入

代码语言:javascript
复制
def encrypt_letter(text_letter , code_letter):
alphabet = string.ascii_uppercase
index = alphabet.find(code_letter)
cypher = alphabet[index:]+alphabet[:index]
index2 = alphabet.find(text_letter.upper())
result = cypher[index2]
if text_letter.islower():
    result = result.lower()
return result

def encrypt(text, code):  
  cypher_text = ''
  for letter in text:
    if letter.isalpha():
        cypher_text += code_word[0:1:6] 
       # code_letter = ?
       # encrypt_letter(letter, )
    else:
        cypher_text += letter
  return cypher_text

code_word = input('Please enter the code word: ')
code_word = code_word.upper()

cypher_text = encrypt(plain_text, code_word)

print(cypher_text)
EN

回答 1

Stack Overflow用户

发布于 2018-10-27 06:42:47

因为这显然是一个学生习题集,所以再花一点时间,然后再回来问一个特定的问题,但有几个提示:

  1. 初始化一个计数器来计算你有多少个字母.isalpha() counter % len(secret_code)来跟踪你在密码中的当前位置
  2. 使用ASCII整数算法和一些if/else逻辑来修改cypher_text中的字符,chr()将帮助你

如果你真的被困住了,需要看看解决方案,我的解决方案在这里:https://github.com/bennett39/learning-exercises/blob/master/cs50/pset6/vigenere/vigenere.py

不过,在单击该链接之前,请在您的解决方案上多花一点时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53017058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档