Python Forum
Unexpected output: if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected output: if statement
#1
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()
Reply
#2
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":
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output when trying to attach files in a mail application PythonU2Novel 0 403 May-17-2024, 02:59 AM
Last Post: PythonU2Novel
  Unexpected output Starter 2 606 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 880 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 615 Oct-24-2023, 05:56 AM
Last Post: buran
Question Common understanding of output processing with conditional statement neail 6 1,029 Sep-17-2023, 03:58 PM
Last Post: neail
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,358 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  Help with output from if statement fgaascht 6 2,113 Jan-17-2022, 02:53 PM
Last Post: perfringo
  unexpected output asyrafcc99 0 1,552 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,133 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Calculation using output of if statement StephenBeckman 3 2,133 Feb-07-2020, 10:19 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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