Ciphering through the non letters

时间:2017-04-10 00:12:24

标签: python python-3.x

So here's my problem...I’m trying to make it so any character not part of the alphabet isn’t shifted in the text file but it does 'take up' a shift amount. For example, if I wanted to implement 2 shift amounts - 3 shifts and 4 shifts - to the statement "alarm clock" the a would be shifted by 3, the l by 4, the second a by 3, r by 4, m by 3, and then the space wouldnt be shifted but would "take up" a space, so it would cause the c to shift by 3. Here's what I have so far

import sys

file = input("Enter input file: ")
shifts = input("Enter shift amounts: ").split()
code = input("Encode (E) or Decode (D)?")
if code == "E":
    with open(file) as f:
        lines = f.read().replace("e","zw").splitlines()
     for line in lines:
        mid = len(line) // 2
        line_list = list(line)
        line_list.insert(mid,'hokie')
        new_line = ('hokie' + (''.join(line_list) + 'hokie'))
        for index, char in enumerate(new_line):
            index = index % len(shifts)

            print(chr(ord(char)+int(shifts[index])),end='')

        print()

example input:

file text:

Though Birnam Wood be come to Dunsinane, And thou opposed, being of no woman born, Yet I will try the last. Before my body I throw my warlike shield. Lay on, Macduff; And damned be him that first cries "Hold, enough!" -- MacBeth

shift:

3 4 5 6 7

result:

kspolWltank Goyqer Drsi icltqpha ivpdb ar Iauvmsguca,nvnmj kspolDri aksz vsttygzh, icltqphantn sk ur butdr hvur,nvnmj kspolBdbz L boso yxf xmfd pmurlifya. Hgzjtxgz re esieoronk kspolL ynyra sf afxsloec vlnnvnmjfdoh. Oed vq, Shfhzlm;ltqph kspolDri kdqsfdg gfd lns wlfz immurliwya gwogzw "Orpi, casubjl!"oronk kspol-- ShkspolfFecakltqph

1 个答案:

答案 0 :(得分:1)

you have to add a if,here my solution.for the punctuation, please lookBest way to strip punctuation from a string in Python

import string
exclude = set(string.punctuation)
shifts = [3,4]
lines = ["alarm clock", "alarm   ';.clock"]
for line in lines:
    mid = len(line) // 2
    line_list = list(line)
    line_list.insert(mid,'hokie')
    new_line = ('hit' + (''.join(line_list) + 'hit'))
    index = 0
    for char in new_line:
        index = index % len(shifts)
        if(char in exclude):
            print(char,end='')
            continue
        print(chr(ord(char)+int(shifts[index])),end='')
        index += 1