Question

Evening/Morning

I've been for the past two days trying to make a program that does a hill cipher. I only have only problem so far and it is that I can't find a way to replace the spaces to 0's. I have tried doing different conditions wich might (or in my case don't) work, I either get nothing shown on screen or the ascii value from space -96. This is what I got so far:

import math
import sys
import string
import numpy as np
from numpy import matrix

msg = sys.argv[1]
key1 = sys.argv[2]
key2 = sys.argv[3]
key3 = sus.argv[4]
key4 = sys.argv[5]
base = sys.argv[6]

key_matrix = np.matrix([[int(key1), int(key3)], [int(key2), int(key4)]])
lettconv = [ord(lett) - 96 for lett in msg]

code might be a little nasty, sorry about that, I'm still a noob with python and programming in general. Anything would be great, I would rather have an explanation on how to do it since it is stuff to do for my uni, I'm pretty lost and could use some guidance.

Cheers!

Était-ce utile?

La solution

So if I am understanding this correctly, in the list comprehension you want to use the value 0 if the letter is a space, otherwise you want to use ord(lett) - 96. You can use a conditional expression to express this logic as follows:

0 if lett == ' ' else ord(lett) - 96

Putting this into the comprehension:

lettconv = [0 if lett == ' ' else ord(lett) - 96 for lett in msg]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top