Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking characters out of list and turning them into other characters

I have made a Secret language that I would to be able to put full words into the input and get the output as the list says the letters should be. Let's say for example I input "AB" I would like the output to be "QW".

while True:
    print("type sentence you want translated")
    Befor=input()
    After=list(Befor)

    if Befor=="A":
        print("Q")
    elif Befor=="B":
        print("W")
    elif Befor=="C":
        print("E")
    elif Befor=="D":
        print("R")
    else:
        print("--")

    print(After)

pass
like image 891
Simone Jones Avatar asked Oct 30 '16 15:10

Simone Jones


People also ask

How do I remove unwanted characters from a list in Python?

Method : Using map() + str.strip() In this, we employ strip() , which has the ability to remove the trailing and leading special unwanted characters from string list. The map() , is used to extend the logic to each element in list.


Video Answer


2 Answers

You're inputting two letters, but your test conditions only contain one character each. You should iterate on the input string using a for and test each character in the string one at a time:

before = input()

for i in before:
    if i=="A":
        print("Q")
    elif i=="B":
        print("W")
    elif i=="C":
        print("E")
    elif i=="D":
        print("R")
    else:
        print("--")

You can also improve your code by using a mapping instead of the if/elif as this will help you accommodate new translations more easily:

before = input()
mapping = {'A': 'Q', 'B': 'W', 'C': 'E', 'D': 'R'}

after = ''.join(mapping.get(x, '--') for x in before)
print(after)

Notice how the dictionary's get method was used to return the default '--' when the mapping does not contain the character.

like image 64
Moses Koledoye Avatar answered Oct 20 '22 04:10

Moses Koledoye


You can auto populate as well,

before = input()

mapping = {chr(x):(chr(x+6) if x + 6 < 91 else chr(x+6-26)) for x in range(65,91)} 

print(mapping)

after = ''.join(mapping.get(x, '--') for x in before)
print(after)
SUBHAM
{'A': 'G', 'B': 'H', 'C': 'I', 'D': 'J', 'E': 'K', 'F': 'L', 'G': 'M', 'H': 'N', 'I': 'O', 'J': 'P', 'K': 'Q', 'L': 'R', 'M': 'S', 'N': 'T', 'O': 'U', 'P': 'V', 'Q': 'W', 'R': 'X', 'S': 'Y', 'T': 'Z', 'U': 'A', 'V': 'B', 'W': 'C', 'X': 'D', 'Y': 'E', 'Z': 'F'}
YAHNGS

[Program finished]

Probably wrap around? This is something like a ceaser cipher? Could just reuse alot of stuff already in python

def genCipher(offset):
  from collections import deque
  from string import ascii_uppercase
  c = deque(ascii_uppercase)
  c.rotate(-offset)
  return dict(zip(ascii_uppercase, c))

print(genCipher(6))
 {A': 'G', 'B': 'H', 'C': 'I', 'D': 'J', 'E': 'K', 'F': 'L', 'G': 'M', 'H': 'N', 'I': 'O', 'J': 'P', 'K': 'Q', 'L': 'R', 'M': 'S', 'N': 'T', 'O': 'U', 'P': 'V', 'Q': 'W', 'R': 'X', 'S': 'Y', 'T': 'Z', 'U': 'A', 'V': 'B', 'W': 'C', 'X': 'D', 'Y': 'E', 'Z': 'F'} 
like image 1
Subham Avatar answered Oct 20 '22 03:10

Subham