-10
print("Hello, this is my first mini-game. Let's test it out")

character = input("Please choose your gender (male, female): ")
age = input("Please choose your age (child, teen boomer): ")
hobby = input("Please choose your hobby(gaming, school, sport): ")

if (character != "male") or ("female") and (age != "child") or ("teen") or ("boomer") and (hobby != "gaming") or ("school") or ("sport"):
    print("Invalid options")
else:
    print("Beautiful choices you are now a {0} that is a {1} and likes {2}".format(character, age, hobby))

This is my code

When I choose the options: male, child and sport it showed invalid options, why??

  • 4
    `(character != "male") or ("female")` does not do what you think it does and there is no reasonable way that it would ever work, especially given how you set the `(...)`. – luk2302 Oct 28 '22 at 13:29
  • 4
    because python is pythoning. – quamrana Oct 28 '22 at 13:31
  • Specifically what's happening in this case... `"female"` is truthy, `"teen"` is truthy, and `"school"` is truthy, so the `if` condition is hard-coded to *always* be `true`. – David Oct 28 '22 at 13:32

1 Answers1

0

You have some mistakes in the way you check the options.

Firstly how you check if a variable has value in a list:

(character != "male") or ("female")

This is incorrect, the first part (character != "male") correctly checks if the value of character is not male, the second part (("female")) though checks the truth value of just the string "female", this will always be true. Also the two conditions should be evaluated with and end (you want to error if character is neither)

To do this check you should do

(character != "male") or (character != "female")

Also python provides a more elegant solution:

character not in ("male", "female")

The second thing is how you put the different conditions together. You are chaining with the and operator but you want to error if any of the three values have wrong choiches so you should use or

The whole code would become:

print("Hello, this is my first mini-game. Let's test it out")

character = input("Please choose your gender (male, female): ")
age = input("Please choose your age (child, teen boomer): ")
hobby = input("Please choose your hobby(gaming, school, sport): ")

if character not in ("male", "female") or age not in ("child", "teen", "boomer") or hobby not in ("gaming", "school", "sport"):
    print("Invalid options")
else:
    print("Beautiful choices you are now a {0} that is a {1} and likes {2}".format(character, age, hobby))
Matteo Zanoni
  • 3,429
  • 9
  • 27