plaintext:sunny day key:like sunny day likelikel <--What I want to get
1)I would like to receive the plain text and key through input value to implement the classic cipher now. Compared to the plain text, I would like to make the key value repeat as long as the plain text as above.
for t in range(len(plaintext)): letterspace = ord(plain[t]) - ord(keyletter[t])
2)Afterwards, we will work to find the distance between ‘s’ and ‘l’, ‘u’ and ‘i’..(continue) However, there is also a problem with this task. This makes indexerror: string index out of range error.
I want to know how to deal with these related two problems. It seems that the answer 1 and 2 are connected, so I post them together.
There are multiple strategies to solve this.
You could repeat the initial key to get a repeated key which is at least as large as the plaintext:
keyletter = keyletter * math.ceil(len(plain)/len(keyletter)) # the following code is not changed for t in range(len(plaintext)): letterspace = ord(plain[t]) - ord(keyletter[t])
You could also use a way to wrap
t when it reaches the end of
keyletter. This can be done with counters, but the easiest would be the use of the modulo operator:
for t in range(len(plaintext)): letterspace = ord(plain[t]) - ord(keyletter[t % len(keyletter)])