Python Forum
Ignore character in if statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ignore character in if statement (/thread-12700.html)



Ignore character in if statement - Fizo - Sep-08-2018

Hello, I am writeing my cipher algorithm and I've problem with my XOR algorithm implementation. How I can ignore character for example comma in IF statement but it's was still been in string. Thanks in advance for help.

for i in range(len(klucz)):
                for x in cipher.find(s,","):
                    if(key_i == x):
                        main_cipher.insert(x,",")
                        #print("Dodaje przecinek na indexie: "+str(x))
                if(key[key_i] != s[key_i]):
                        main_cipher.append("1")
                        #print("Dodaje 1 na indexie => "+str(key_i)+" z porówniania cyfry klucza: "+str(klucz[key_i])+", oraz tekstu: "+str(s[key_i]))
                        key_i += 1
                else:
                        main_cipher.append("0")
                        #print("Dodaje 0 na indexie => "+str(key_i)+" z porówniania cyfry klucza: "+str(klucz[key_i])+", oraz tekstu: "+str(s[key_i]))
                        key_i += 1



RE: Ignore character in if statement - ichabod801 - Sep-08-2018

Don't iterate over range(len(something)) (here is a more detailed explanation of why not). Here you seem to be iterating over two things, so you can xor them. That's what zip is for. zip('ABC', '321') creates a generator that outputs [('A', '3'), ('B', '2'), ('C', '1')]. So:

for plain_char, key_char in zip(plain_text, key):
    if plain_char == key_char:
        cipher.append('1')
    else:
        cipher.append('0')
To not encode punctuation, just add another if clause at the beginning (and make the first if clause an elif):

if plain_char in ',.!?':
    cipher.append(plain_char)



RE: Ignore character in if statement - Fizo - Sep-08-2018

I do not know if we understood each other well, I mean that the condition should check every element of plaintext and key and when it encounters a comma it skips it and no append is executed


RE: Ignore character in if statement - ichabod801 - Sep-08-2018

Oh, the main_cipher.insert call confused me. If you don't want to insert it, just use pass or continue there.


RE: Ignore character in if statement - Fizo - Sep-08-2018

Unfortunately, I want to insert a comma, and I have a problem, how can I make it invisible to the if statement, but it was still physically. Sory for not precisly descibe my problem


RE: Ignore character in if statement - ichabod801 - Sep-08-2018

I'm completely lost by what you want to do with the comma, or what you mean by "invisible to the if statement." Perhaps you could provide a short example of inputs and outputs so I could see what you are trying to do.


RE: Ignore character in if statement - Fizo - Sep-09-2018

I'm so sory that you are fell lost. Fortunatly with your litle help i solved my problem. Have nice day :)