How to have two if statements to point towards the same variable as output

I wrote a short “custom” story where you give your name and the computer makes you the main character of the story. One of the questions is also to name a person you haven’t talked to in a while. That person ends up being your boss in the story. At one point the boss forgets HIS/HER wallet. Also, the main character (you) tries to solve the day using HIS/HER superpower.
In the final output I have defined both genders as

print(Bossname, "accidentally forgot", GENDER, "wallet and tried looking for it. Then", yourname, "tried to find it using", GENDER, "superpowers.") 

Here, the GENDER represents both the boss’ gender as well as your gender.

Frank accidentally forgot HIS wallet … Emilia tried to find it using HER superpowers.

The only idea I have with and or or would either require both characters to be the same gender, or would assume they are the same gender if only one of them is that gender, so that’s not the solution :smiling_face_with_tear:

I have made the program decide the gender based on the last letter of the name (it is common for women in this region to have letters ending in letter “a”). For that reason, there is an if statement that checks if the last letter of the string is “a”.

I know this could be solved with GENDER_BOSS and GENDER_YOURNAME but I would like to make it just one variable, where two different if statements would lead to the same line, something like this:

image

Why not use a dictionary?

GENDER = {"boss": "unset", "you": "unset"}
if Bossname[-1] == "a":
    GENDER["boss"] = "her"
else:
    GENDER["boss"] = "his"
if Yourname[-1] == "a":
    GENDER["you"] = "her"
else:
    GENDER["you"] = "his"
3 Likes

Not gonna lie, I started learning less than 10 days ago, I am not sure what dictionaries are hahah. I will check it up

Here’s a w3schools page on dictionaries, as well as a python docs page on them.

2 Likes

You are amazing!
I’ll check them out

1 Like

Follow-up, is there no way to put GENDER outside of the conditional so you don’t write it twice GENDER == “his”
GENDER == “her” ?

@HatidzaSerdare1 Try making a list of two Strings, like this:

genders = [] # Creating the list
if Bossname[len(Bossname)-1] == "a": # Checking if the last letter of pre-defined var "Bossname" is 'a'
  genders.append("her") # If it is, add 'her' to the list
else:
  genders.append("his") # If not, add 'his' to the list
if yourname[len(yourname)-1] == "a": # Checking if the last letter of pre-defined var "yourname" is 'a'
  genders.append("her") # If it is, add 'her' to the list
else:
  genders.append("his") # If not, add 'his' to the list
print(Bossname, "accidentally forgot ", genders[0], " wallet and tried looking for it. Then ", yourname, " tried to find it using ", genders[1], " superpowers.")
# To access items in a list, just do genders[WHATEVER_INDEX]
# A index is the number associasted with a certain item in a list. Indexs start at 0.

2 Likes

Isn’t that what would happen if you used your current approach?


You mean strings?

Yeah. Wasn’t thinking. I was going to originally put a Boolean in the list, but decided against it.


No, because the current program assumes that both the boss and player’s character have the same gender.

1 Like

I suppose you could do this:

GENDER = {
    "boss": "his" if Bossname[-1] == "a" else "her",
    "your": "his" if Yourname[-1] == "a" else "her"
}

Though that still results in two if statements, they are more compact.

1 Like

I’d recommend making a function.

def get_gender(name):
  return "her" if name[-1] == "a" else "his"

Then create a dictionary using the names as keys. (Assumes your name is not the same as the boss’s name). Also, f-strings are better than string concatenation:

GENDER = {name: get_gender(name) for name in [Bossname, yourname]}
print(f"{Bossname} accidentally forgot {GENDER[Bossname]} wallet and tried looking for it. Then {yourname} tried to find it using {GENDER[yourname]} superpowers.")

Well, it’s actually not even necessary to store the genders at all if you don’t mind some runtime overhead because you can just get it from the name. Less clutter.

print(f"{Bossname} accidentally forgot {get_gender(Bossname)} wallet and tried looking for it. Then {yourname} tried to find it using {get_gender(yourname)} superpowers.")