I'm currently trying to make a cipher program, here is my code;
import string
import random
matrix = []
codedmessage = []
letter = "ABCDEF"
message = input("Enter message to be encoded:\n").upper().replace(' ', '')
print ('---MESSAGE---\n', message)
newlist = list(string.ascii_uppercase + string.digits)
random.shuffle(newlist)
print ('---MATRIX---')
for x in range(0,len(newlist),6):
matrix.append(list(newlist[x:x+6]))
for letter in message:
for y, vector in matrix:
for s, member in vector:
if letter == member:
codedmessage.append(letter[x], letter[y])
for i in range(len(matrix)):
print(matrix[i])
However, when I compile this I get the error;
for y, vector in matrix: ValueError: too many values to unpack (expected 2)
Can anyone shed some light on this as to why it is happening and give a solution?
Thanks
matrix.append(list(newlist[x:x+6]))
You append 6 element lists to matrix
, but you try to unpack them into two variables later:
for y, vector in matrix:
The numbers have to match.
Currently you matrix
looks like [ [4,3,2,6,3,2], [2,1,6,8,9,2], ... ]
. How is python supposed to unpack one of the elements, for example [4,3,2,6,3,2]
into y
and vector
? What should go where? (For possible solutions see the other answers, they were faster. I don't understand what behaviour is expected anyway.)
Also you cannot index a character:
codedmessage.append(letter[x], letter[y])
previously you assigned a single character to letter
, here:
for letter in message:
because message
is a string. You probably confuse names as you already have assigned a string to letter
initially: letter = "ABCDEF"
Probably you want to use two different names.
append
does only take one argument, too. Again I don't know what you expect, but I guess it should be either codedmessage.append([letter[x], letter[y]])
or codedmessage += [letter[x], letter[y]]
.
I also highly doubt that you want to use x
in codedmessage.append(letter[x], letter[y])
because you only used x
in another independent loop as iteration variable.