jeudi 20 février 2020

Asking for user input between 2 genders using a while loop and to be stored in a variable?

Want to create a small program that can ask a user a question. Do you want your character to be Male or Female? And output the result. However as you can tell the results are poor from my side.

Can someone give me a hand in fixing this mess as I feel I have over complicated it.

Just some general rules that the program must have:

  • It must accept both "Male" and "male" as input and vice versa for female
  • The user input must be stored in the variable created.
  • If the user types anything else that is not: 'female' 'Female' 'male' or 'Male' they must try the question again (which is why I put in a while loop)

It's strange but this should be simple for me normally but I've made a bit of a mess of it.

Any help would be appreciated :)

gender = None

gender = str(input("Enter your Gender1: "))
while gender != 'Male' or 'male' or 'female' or 'Female':
    gender = input("Enter your Gender2: ") 
if gender in ['Male', 'male']:
    print("You have chosen Male")
if gender in ['Female', 'female']:
    print("You have chosen Female")

print(gender) 

I don't mind the whole method changing where options can be used such as:

  • Press 1 for 'Male' or 2 for 'Female'

Would be of a help :)

Aucun commentaire:

Enregistrer un commentaire