我是一个学生在做我的课程,我们必须作出一个维格纳密码程序。 我知道我的程序是复杂的,但我不知道有任何其他方式来做它和它来不及修复。 我似乎有一个问题,当我添加的序数值的消息和关键字的新的序数值是超出范围的正常字母值。所以它会打印出像这样奇怪的字母。你知道吗
这是我的密码:
newmessage1 = []
stringposition = 0
number1 = 0
mesletter = 0
keyletter = 0
output =[]
keylist = []
stringofnumbs = []
question = input('Would you like to encrypt, decrypt a message or quit?')
encdec=[]
if question == 'encrypt'or'encrypt a string'or'ENCRYPT'or'encrypt_a_message'or'Encrypt':
message1 = input('Please input a message to encrypt')
keyword1 = input('Please input a keyword ')
if message1.isalpha and keyword1.isalpha:#this check if the message and the keyword only contains letters
messagelength=len(message1)#this sets the variable 'messagelength' to the length of message1
newkeyword=''#this is an empty variable used to insert the length of the new keyword
while len(keyword1)<=len(message1):#this will loop checks that the length of keyword1 is smaller thasn the length of message1.
keyword1+=keyword1#this will repeat the keyword to fit the length of the message
newkeyword=keyword1[:len(message1)]
#this set the 'newkeyword' variable to the length of the new keyword
#(the new keyword is the keyword repeated to fit the length of the message.)
for mesletter in message1:
mesnumber = ord(mesletter)#what it does is it takes every letter in message1 and puts it into its unicode form.
stringofnumbs.append(mesnumber)#once one letter has been put in its unicode value it will append that unicdoe from of that
#letter in the variable 'stringofnumbs',it will do this process for every letter in the message.
for keyletter in keyword1:
keynumber = ord(keyletter)#what it does is it takes every letter in keyword1 and puts it into its unicode form.
keylist.append(keynumber)#once one letter has been put in its unicode value it will
#append that unicdoe from of that letter in the variable 'stringofnumbs',it will do this process for every letter in the message.
temp1 = int(stringofnumbs[stringposition])#temp1 is a variable that holds the ordinal values of letters in message1
temp2 = int(keylist[stringposition-1])#and temp2 is a variable that holds the ordinal values of letters in the keyword1
temp3 = temp2+temp1#temp3 then adds these ordinal values togther
encdec.append(temp3)#the ordinal values added togther are appended to the encdec variable
for number1 in encdec:
newletter1=chr(number1)#this for loop takes every letter in encdec and puts the ordinal values back into charcters
newmessage1.append(newletter1)#then every letter that has been changed to a charcater value is appended to the varibale newmessage1
print (' ')#this leaves a bit of space to make the encoded text clear
print ('the endcoded text is:')#this is just a bit of text to make the encoded text clear to the user
print (''.join(newmessage1))#here the encoded message is printed
else:
('The message or keyword is invalid please use only numbers')#this will print only if the keyword or message doesnt only contain letters and spaces
你用序数使事情变得不必要的复杂化。实际上,你使用的“字母表”有256个符号,而你只对26个感兴趣。你知道吗
理想情况下,你只需要你的符号在0到25之间的数字范围内,一旦你达到26,你会再次扭曲到0。你可以用序数来实现这一点,但是你必须使用正确的偏移量。你知道吗
现在,假设您只使用大写字母。它们的序数从65到90不等。所以一旦你到了91,你就想转到65。你知道吗
另一个问题是您只需添加
temp1
和temp2
。temp1
可以保持为介于65和90之间的数字,但是temp2
应该是键偏移量,范围从0到25。因此,当您计算temp2
时,您真的应该这样做这将从概念上解决您的问题,但您的算法仍然不正确。while循环的目的是将关键字的长度至少扩展为消息的长度。其他所有内容必须在左边缩进4个空格,这样它们就不是循环的一部分。明确地说
在上面的代码块中,我想指出我纠正的更多错误。你应该迭代
newkeyword
,而不是keyword1
,而且你还必须增加每个字母的stringposition
计数器。你知道吗对于一个更有效、更不复杂的算法,可以考虑将代码发布到Code Review,以便进行回顾,并从得到的反馈中学习。你知道吗
虽然不是编写代码的最佳方法,但在保持代码不变的同时,仍然可以进行一些基本的整理。基本上,一旦你的消息和关键字的长度相同,你就可以遍历这对字母,然后计算加密的字母,这样就可以把它存储在一个列表中。没有理由存储中间步骤,例如将消息和关键字转换为数字列表。你知道吗
我认为解决这个问题最常见的方法是把数字再“包装”一下。也就是说,如果除以要允许的最大有效序数,然后使用余数。你知道吗
例如,使用
chr
作为ord
的倒数:我真的不知道这会影响你以后的描述,我还没有调查过,但你可能会想一想。你知道吗
相关问题 更多 >
编程相关推荐