Python Forum

Full Version: Ignore character in if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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)
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
Oh, the main_cipher.insert call confused me. If you don't want to insert it, just use pass or continue there.
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
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.
I'm so sory that you are fell lost. Fortunatly with your litle help i solved my problem. Have nice day :)