0

I am making a project and was wondering how I could use code to tell if the user has entered something other than "Male", "Female", "male", or "female".

I am using Python 3.0

This is my code:

 import time
def w(t):
  if t == t:
    time.sleep(t)
print("In the following story, you are a mouse. Please fill out the following information (the story will use this name and gender).")
w(3)
print('\n')
name = input("Your Name:")
w(0.5)
print('\n')
gender = input("Your Gender (Male / Female):")
w(0.5)
print('\n')
input("Press Enter to Begin The Story")
w(1)
print('\n')
print("The Lion and The Mouse")
print("A Short Tale by Aesop")
w(1)
print('\n')
print("Once upon a time in a secluded jungle, there was a mouse.")
w(2)
print('\n')
if gender == ("male" or "Male"):
    heorshe = "he"
    hisher = "his"
    himher = "him"
    himselfherself = "himself"
    HeShe = "He"
if gender == "female" or "Female":
    heorshe = "she"
    hisher = "her"
    himher = "her"
    himselfherself = "herself"
    HeShe = "He"

I would also like to be able to tell if the user has entered in nothing into the input! Please help!

  • `gender == ("male" or "Male"):` is not how you check. You need to compare gender against both individually. A better plan though is to lowercase the input first, then only check against "male". – Carcigenicate Jun 06 '17 at 15:22
  • Also, what is `if t == t:` supposed to accomplish? I only know of a single value that that will fail for. – Carcigenicate Jun 06 '17 at 15:23
  • @Carcigenicate Thank you! I will try converting the answer into lowercase and see if it runs correctly! But if you know the answer as to how to tell if the input is empty or numerical values, please let me know! – Still Here Coding Jun 06 '17 at 15:27
  • To check if it's empty, just compare against "". That shouldn't be necessary though, as you're already checking if it's male/female. If it's equal to either of those, it can't be empty. Also, I believe Python has a `isNumeric` function (or something like that) that checks if a string is numeric. You can also just try to parse it and catch the exception that's thrown. Sorry, my breaks over now. I'd write an answer if I had more time. – Carcigenicate Jun 06 '17 at 15:30
  • @Carcigenicate Thank you very much! Your help is appreciated :) – Still Here Coding Jun 06 '17 at 15:31
  • I would turn get_gender() into a function so the program doesnt terminate early if an invalid selection is entered. Also, I advised your don't go down the rabithole of gender neutral pronouns. Finally, what about using a pronoun dictionary in your code to hold the different contextual pronouns? You can the nust do if male -> make male_pronoun_dict, else -> make female_pronoun _dict – syntaxError Jun 06 '17 at 17:46

1 Answers1

0

You could just use else:, or if not 'male' or not 'Male:' and then print for example 'Please enter Male or Female.'

optimalic
  • 511
  • 3
  • 17
  • This didn't work when I tested it. When I ran the input and pressed enter without typing anything, it didn't run the code: print("Please enter Male or Female"). Please elaborate! – Still Here Coding Jun 06 '17 at 16:29
  • My breaks over, I have to go, but I will be back on around 3:00 pm EST to check your response or answer. – Still Here Coding Jun 06 '17 at 16:32
  • The problem is it does run the print statement but after you check what the user entered and because that's on the end of the code the story just starts. But the first answer of this [post](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) should work for you too. – optimalic Jun 06 '17 at 17:30