我应该制作一个行为喜欢这个的程序:
$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():
当我运行它时,它会要求我输入一条消息,但是返回说第51行中的语法错误,在<module> main(
中)和第38行,
main text = input("Type a message: /n"))
File "<string>", line 1
不知道为什么你使用main text = ...
INSTEAD只是text = ...
或main_text = ...
而且看起来你最后还有一个额外的括号....
所以,
如果你想从用户那里得到一个字符串并将其存储在变量text
中,你应该像这样重写你的语句:
如果您使用的是Python3:
text = input("Type a message: ")
如果您使用的是Python2:
text = raw_input("Type a message: ")
请指定您下次询问时使用的python版本,以便我们更容易回答:)
(您可以使用import sys; print(sys.version)
检查您运行的版本)