Problem with if, elif, else

I need some help with the following code…
When I run it the else function keeps executing when I don’t want it to.

#imports
from Words import Eng_word, Ger_word, Error
import random

# Language
print('Please select your language, (English or German).')
print('Noote that this program is caps sensitive.')
print(' ')
language = str(input("Language: "))


if language == "German":
    t = (Ger_word) 
    t.Test()

if language == "English":
    t = (Eng_word) 
    t.Test()

else :
    t = (Error)
    t.Test()

The following code are the seperate files I am importing…

def Test():
    Test = f"English"

    print(Test)
def Test():
    Test = f"German"

    print(Test)
def Test():
    Test = f"Error!"

    print(Test)

The end goal for this program is to act like a bit of a translator… It will draw and translate words from a word bank (words they already know).

Please note that the if language == "English" and else clauses in your code form together a single if-else compound statement – separate from the if language == "German" compound statement.

That is, you have two separate compound statements, executed one after another: (1) if (the “German” part) and (2) if-elif (the “English” part with the error part).


What you need is a single compound statement, if-elif-else. To have one, you just need to replace the fragment:

  • if language == "English"

with:

  • elif language == "English"

– transforming that clause into a contituation of the if language == "German" statement. Then you will have:

...

if language == "German":
    ...
elif language == "English":
    ...
else:
   ...

(I replaced irrelevant code fragments with ...)


Note: elif is just an abbreviation for the words: else if (but they are not a legal syntax in Python).

1 Like

Thanks… I have fixed it up now and it runs fine. :hugs:

One more thing I would like to know is if there is a way to re-run the code if the else statement is true…

#imports
from Words import Eng_word, Ger_word, Error
import random

# Language
print('Please select your language, (English or German).')
print('Noote that this program is caps sensitive.')
print(' ')
language = str(input("Language: "))


if language == "German":
    t = (Ger_word) 
    t.Test()

elif language == "English":
    t = (Eng_word) 
    t.Test()

else :
    t = (Error)
    t.Test()

For example, If the terminal returns “Error” it will run the code again.

You can do it by adding a while loop:

#imports
from Words import Eng_word, Ger_word, Error
import random

# Language
while 1: # This loop will run forever
    print('Please select your language, (English or German).')
    print('Note that this program is caps sensitive.')
    print(' ')
    language = str(input("Language: "))


    if language == "German":
        t = (Ger_word) 
        t.Test()
        break # Exit the loop because the language selection is valid

    elif language == "English":
        t = (Eng_word) 
        t.Test()
        break # Exit the loop because the language selection is valid 

    else :
        t = (Error)
        t.Test()
1 Like

You can also take advantage of a match statement:

match language:
    case 'German':
        t = (Ger_word)
    case 'English':
        t = (Eng_word)
    case _:
        t = (Error)
t.test()

This would have the same functionality as the if/elif/else above and be more readable if you add additional options (languages.)

You enter this function with the name Test referring to the function, and then immediately overwrite it with a string. You should not repeat names like that, as you may find yourself looking for one and getting the other. In this specific case, the scope of the name is limited to the function, however that may not always be the case.

1 Like