1

This is the code I have...

male = "male"
female = "female"

role = input("Are you a male or female? ")

if role == male:
    print("male")

if role == female:
    print("female")

Results: Are you a male or female? male male Are you a male or female? female female Are you a male or female? nale (nothing returned)

*If you misspell a word in your input response is there a way for your code to print something else?

I've tried to adding new else statements, a new 'typo' variable, then add an elif statement.

ex:

male = "male"
female = "female"
typo = not "cat" or "dog"

role = input("Are you a male or female? ")

if role == male:
    print("male")
elif typo:
    print("over")

if role == female:
    print("female")
elif typo:
    print("over")

Results: Are you a male or female? male male over Are you a male or female? female over female Are you a male or female? nale over over

hackerboi
  • 31
  • 5
  • 4
    You might want to look into [fuzzywuzzy](https://github.com/seatgeek/fuzzywuzzy) – Green Cloak Guy Feb 13 '20 at 18:32
  • Are you looking to look for only a specific set of typos, or do you just want to only allow certain values? – G. Anderson Feb 13 '20 at 18:34
  • I'm just looking to have it print "over" if the value "cat" or "dog" is not entered for the question. @G.Anderson – hackerboi Feb 13 '20 at 18:36
  • @GreenCloakGuy Okay thanks, I will – hackerboi Feb 13 '20 at 18:36
  • 3
    Warning: `typo = not "cat" or "dog"` doesn't do what you think it does. Inspect `typo` right after running that. – ChrisGPT was on strike Feb 13 '20 at 18:38
  • @Chris Is setting typo = to variables the same thing as setting it = to text? – hackerboi Feb 13 '20 at 18:43
  • I second fuzzywuzzy – it gives you the number of edits necessary to change one word to another. Thus, `"nale" -> "male"` is only a single change, as well as `"mael"`. But with your small choice of options it cannot handle `"emale"`. – Jongware Feb 13 '20 at 18:56
  • @usr2564301 is there no way to just have python say if user input isn't cat or dog or male of female... then print or do this... because like what if someone where to type "jhsdfdsjfsdfjsffsdf" or something dumb.. – hackerboi Feb 13 '20 at 19:01
  • @hackerboi To attempt and simplify it. Nonempty strings in python are considered truthy. The statement `'cat' or 'dog'` equates to `'cat'` since it's checking a conditional where both are present. It looks at `'cat'` first and sees it's present so it hands that back to you. `not 'cat' or 'dog'` equates to the opposite of that so it equates to `'dog'`. – Axe319 Feb 13 '20 at 19:04
  • So essentially `typo = not "cat" or "dog"` is saying `typo = "dog"`. – Axe319 Feb 13 '20 at 19:04
  • You are asking about *typos* but it seems you want a whitelist of allowed answers only. – Jongware Feb 13 '20 at 19:06
  • role = " " valid_roles = ['male', 'female'] role = input("Are you a male or female? ") if role in valid_roles: print(role) else: print("over") – hackerboi Feb 13 '20 at 19:30

1 Answers1

1

If i understand your question correct here is something you can do:

your_input = 'male'
valid_inputs = ['cat', 'dog']
if your_input in valid_inputs:
    print(your_input)
else:
    print('over')
  • YES! This is it! Thank you! Sorry for not explaining it that well guys! – hackerboi Feb 13 '20 at 19:29
  • role = " " valid_roles = ['male', 'female'] role = input("Are you a male or female? ") if role in valid_roles: print(role) else: print("over") – hackerboi Feb 13 '20 at 19:29