我应该制作一个行为如下的程序:
$Python
vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs fsck rmwse bh auebwsih!
使用维杰尼雷密码
我将使用一个辅助函数并将其导入到这个函数中,我知道它有效
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
在那之后,我开始加密它的维杰尼雷部分
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():
当我运行它时,它会要求我输入一条消息,但返回说第 51 行在<module> main(
中存在语法错误(和 第 38 行,在
main text = input("Type a message: /n"))
File "<string>", line 1
不知道为什么你用main text = ...
而不是text = ...
或main_text = ...
,而且看起来你最后还有一个额外的括号......
所以
如果你想从用户那里获取一个字符串并将其存储在变量text
你应该像这样重写你的语句:
如果您使用的是Python3:
text = input("Type a message: ")
如果您使用的是Python2:
text = raw_input("Type a message: ")
下次询问时,请指定您使用的python版本,以便我们更容易回答:)
(您可以检查您正在运行的版本import sys; print(sys.version)
(