Python Forum
Ignore character in if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignore character in if statement
#1
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
Reply
#2
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
Oh, the main_cipher.insert call confused me. If you don't want to insert it, just use pass or continue there.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I'm so sory that you are fell lost. Fortunatly with your litle help i solved my problem. Have nice day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ignore WakeWord after it's said Extra 2 1,137 Apr-01-2022, 12:32 AM
Last Post: Extra
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,635 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  [solved] unexpected character after line continuation character paul18fr 4 3,292 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,106 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Ignore first few letters of a line when reading file. ShakeyPakey 16 6,180 May-30-2020, 02:17 PM
Last Post: BitPythoner
  How to ignore empty columns from DB? Winfried 1 2,245 May-15-2020, 08:35 PM
Last Post: menator01
  How to make the script ignore down devices. wagnergt12 4 3,130 Apr-20-2020, 11:45 PM
Last Post: wagnergt12
  Regex ignore JohnnyCoffee 1 2,550 Mar-16-2020, 12:16 PM
Last Post: scidam
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,673 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Ignore Folder Evil_Patrick 3 3,641 Oct-29-2019, 07:44 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020