Python Forum

Full Version: Unexpected output: if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am pretty new to python, and I am trying to make a encrypter/decrypter, but my "if statement" is not working. Every time i run the program, it runs the first option("if action == "Krypter" or "krypter" :). I don't understand what I have done wrong here, please help.

alphabet = "abcdefghijklmnopqrstuvwxyzæøå"
length = len(alphabet)


action = input("Krypter eller Dekrypter: ")


if action == "Krypter" or "krypter" :

    def encrypt(message,key) :
        
        def encode(letter, key) :
            pos = alphabet.find(letter)

            newpos = (pos + key) % length

            return alphabet[newpos]

        
        output = ""


        for character in message :
            if character in alphabet :
                output = output + encode(character, key)
            else:
                output = output + character


        print(output)
    
    encrypt(input("Krypter: "), int(input("Krypteringsnøkkel: ")))

elif action == "Dekrypter" or "dekrypter" :

    def decrypt(secret_message, key) :
        
        def decode(letter, key) :
            pos = alphabet.find(letter)

            newpos = (pos - key) &length

            return alphabet[newpos]


        output = ""


        for character in secret_message :
            if character in alphabet:
                output = output + decode(character, key)
            else :
                output = output + character


        print(output)
    
    decrypt(input("Dekrypter: "), int(input("Dekrypteringsnøkkel: ")))

else :
    exit()
if action == "Krypter" or "krypter":
this if condition is always True
what you want is
if action == "Krypter" or action == "krypter":
but there are alternatives
if action in ["Krypter", "krypter"]
or
if action.lower() == "krypter":