Changing characters from a python message

Asked

Viewed 327 times

-1

I want to make a program that encrypts a message by the polar Zenit method, which consists of exchanging these letters among themselves, changing the text without the use of accents. I can already change the letters of the first group (Zenit) to those of the second group (polar), and scramble a little the text, but not the other way around because if I just reverse what I already wrote the same input message will be returned. I imagine you can use lists or dictionaries in a better way, but I don’t know how.

A summary of what I have so far:

texto = input("Digite um texto: ")
lista_z = ['z', 'e', 'n', 'i', 't']

for z in lista_z:
    texto = texto.replace('z', 'p')

for e in lista_z:
    texto = texto.replace('e', 'o')

for n in lista_z:
    texto = texto.replace('n', 'l')

for i in lista_z:
    texto = texto.replace('i', 'a')

for t in lista_z:
    texto = texto.replace('t', 'r')

print(texto)

I’m starting now and I really want this little help to find a solution, from now on thank you.

  • The idea of your for is wrong. you can replace all these for meaningless by function of string class, I suggest a studied

  • 1

    Friend, can you give an example for me to understand better?

1 answer

2


Your for is running through the lista_z but does nothing pq Voce does not use his variable for anything.

to understand how the for

lista_z=['z','p','x','c','v']

for z in lista_z:
     print(z)

this way will print each item on the list, quite different from what you were doing.

another thing is to look at the functions that the string class has. that link can help you understand what a string is.

this is to understand first how to go through a list item by item.

in its code can be applied as follows, I used dictionary, but has the same idea in the for to browse the list

texto = input("Digite um texto: ") 
dic_z = {'z':'p', 'e':'o', 'n':'l', 'i':'a', 't':'r'}

for z in dic_z: 
     texto = texto.replace(z, dic[z])
  • @Lucas, I put up the part to replace the characters, I believe that now you will understand how the for and can apply in the reordering of the sentence. Any doubt you speak

  • I think I figured out your example and how to use the for, but I still can’t fully apply it. When I write something with the first group’s lyrics, the Zenit, everything works, but if I create a dictionary with the reverse of dic_z so that the group polar also be translated, then I get my "input" as a response. You would have some hint to prevent what has already been translated from going through for again or similar resolution?

  • face i advise to use functions. one for the Zenit and the other for polar. link to understand how to use function.

  • I believe you’re starting in programming. I’m sorry if I made a mistake, a piece of advice I give is for those who are starting it is to focus on data structure

  • 1

    Yes, I will go deeper into these structures and continue with the code later. Thank you for the direction!

  • continue with the code, it is a good exercise. studies if for example, it will help you now not to go through for. as long as you understand you apply it to him

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.