Vigenere Cipher语法错误

时间:2018-08-31 14:56:29

标签: python vigenere

我应该制作一个行为类似于这样的程序:

$Python 
vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs fsck rmwse bh auebwsih!

使用Vigenere密码

我要使用一个辅助函数,并将其导入该函数,该函数我知道是可行的

import string

alphabet_pos = "abcdefghijklmnopqrstuvwxyz"
def alphabet_position(letter):

    pos = alphabet_pos.index(letter.lower())
    return pos 

def rotate(letter, rot):
    pos = alphabet_position(letter)
    new_pos = (pos + rot) % 26
    new_char = alphabet_pos[new_pos]

    return new_char

之后,我开始加密其中的Vigenere部分

from helpers import alphabet_position, rotate
from caesar import encrypt

    def encrypt(text,key):
        #Declare variable
        cipher = ''

        #Compute length
        l = len(key)

        #Assign value
        idx = 0

        #Loop
        for i in text:
            #if condition satisfies
            if i.isalpha():

                #Call method
                cipher += rotate_character(i,alphabet_position(key[idx]))

                #Update to next 
                idx = (idx+1)%1
            #Otherwise
            else:

                #Increment
                cipher += i

        #Return
        return cipher
    #Define main
    def main():

运行此命令时,它将要求我键入一条消息,但返回说<module> main(的第51行中有语法错误,并且

中的第38行
main text = input("Type a message: /n"))
File "<string>", line 1

1 个答案:

答案 0 :(得分:0)

不确定为什么您只使用main text = ...text = ...的{​​{1}} INSTEAD,而且看起来最后还有一个括号。...

所以

如果您想从用户那里获取一个字符串并将其存储在变量main_text = ...中,则应该这样重写语句:

如果您使用的是 Python3

text

如果您使用的是 Python2

text = input("Type a message: ")

请在下次询问时指定您正在使用的python版本,以便我们轻松回答:)

(您可以检查使用text = raw_input("Type a message: ") 运行的版本)

相关问题