pythonpython-3.xif-statementlogic

Elif Statements Ignored Despite Conditions Met? (Python 3.12)


For whatever reason, no matter what input I provide, the program defaults to opt. A.

So, I'm a beginner learning Python (3) independently, and I've ran into a very confuddling problem. In my mind, I figured the order of operations was quite straightforward; I figured: "I'll create a variable to get player input. Then, that information will go into the variable 'Pronoun_Decision.' From there, the computer just has to compare the variable's value with the if/elif statements and execute accordingly!"

However, it didn't quite go that way... instead, no matter what gets typed, it always executes the statements for opt. a. Since VS Code isn't displaying an error messages, I figure whatever the problem is, it isn't a syntax error, therefore it must be some kind of issue with the order of commands. But I feel like the order of commands make sense, does it not? Like, after learning the player's name, the player must type either "a, b, c," or "d," and whatever they type should go into a variable (Pronouns_Choice). From there, it should be permenantly stored into another variable (Pronoun_Decision) and execute the corresponding set of statements based on player input.

What have I done wrong, exactly? I've read over some of the posts centered around similar questions, but I feel like the issues the OPs for those other questions were facing are a little too distinct from mine, so I don't really get how to apply the answers they were provided to my own situation. Any advice would be much appreciated!! I will provide images of the code/output as well as type it manually.

First half of the code: (https://i.sstatic.net/M6DMVutp.png)

Second half of the code: (https://i.sstatic.net/5yI7w4HO.png)

Output: (https://i.sstatic.net/k9OaBsb8.png)

Gametitle = "Conversation with a Computer Program"
GameGuider = "Guide.AI"

print(f"Good evening, Player. Welcome to the homescreen.\n")
print(f"This is where you may decide if you'd like to proceed or not. My name is {GameGuider.upper()}.\nWhat would you like to be called?\n")

NamePrompt = input()
CharaName = NamePrompt
A_Pronouns = "She/Her/Hers"
B_Pronouns = "He/Him/His"
C_Pronouns = "They/Them/Theirs"

print(f"\n{NamePrompt}...\n")
print(f"{GameGuider}: I see. It is a pleasure to make your acquaintance, {CharaName}. And by what pronouns should I call you?\n\n")
print(f"A: {A_Pronouns}\nB: {B_Pronouns}\nC: {C_Pronouns}\nD: Other (User Input)")

Pronouns_Choice = input()
Pronoun_Decision = Pronouns_Choice

if Pronoun_Decision == "A" or "a":
    print(f"I see. So, you are called {CharaName} and your pronouns are {A_Pronouns}. Wonderful to know!\n")
elif Pronoun_Decision == "B" or "b":
    print(f"I see. So, you are called {CharaName} and your pronouns are {B_Pronouns}. Wonderful to know!\n")
elif Pronoun_Decision == "C" or "c":
    print(f"I see. So, you are called {CharaName} and your pronouns are {C_Pronouns}. Wonderful to know!\n")
elif Pronoun_Decision == "D" or "d":
    print("Please provide your pronouns.\n")
    D_Pronouns = input()
    E_Pronouns = D_Pronouns
    print(f"I see. So, you are called {CharaName} and your pronouns are {E_Pronouns}. Wonderful to know!\n")

#For some reason, the program defaults to the Opt. A pronoun choice, even if I type some other letter. Why is that?

print(f"Well, given that I am an AI program, I have no sex or gender. So, I may be referred to by whatever pronouns you\'d like to use!\n")
print(f"Let us move on.\nWelcome to your {Gametitle} experience!")


Solution

  • Change

        if Pronoun_Decision == "A" or "a":
    

    to:

        if Pronoun_Decision == "A" or Pronoun_Decision == "a":
    

    or:

        if Pronoun_Decision in ["A","a"]:
    

    It is currently evaluating two statements:

        if Pronoun_Decision == "A", or
        if "a":
    

    if 'a': always returns True