0

I have been typing this code and the answer I came out with is it is a fish if I type in no

z=input('Does the creature have gills? ')
if z==('yes') or ('yeah'):
  print ('It is a fish!')
elif z=='no':
  print('The creature is not a fish.')
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Does this answer your question? [Why won't the while loop continue and stop after a user question?](https://stackoverflow.com/questions/61843745/why-wont-the-while-loop-continue-and-stop-after-a-user-question) – David Buck May 21 '20 at 10:10
  • My answer to the question I've just linked shows you how to do this (although that question was closed as a duplicate, the answers to the question I linked are a bit simpler). – David Buck May 21 '20 at 10:11
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – 0buz May 21 '20 at 10:21

1 Answers1

4

if z == 'yes' or ('yeah') is equivalent to if (z == 'yes') or ('yeah')

means if the input is 'yes' or TRUE, which always evaluate to true.

what you need to do instead is if (z == 'yes') or (z == 'yeah') Or you can just remove the parentheses

z = input('Does the creature have gills? ')
if z == 'yes' or z == 'yeah':
    print('It is a fish!')
elif z == 'no':
    print('The creature is not a fish.')

Some other things you can do is to make an array of the values that you want to validate, and check if a value exists

Or you can create a dictionary with the values as the key, then check if the dictionary has the value exists as a key

But a simple if statement should be enough for now.

Mr.Cappuccino
  • 106
  • 1
  • 9
  • 1
    There's a lot more that cound be done to improve this answer. There's no need to have parentheses around 'yes' and 'yeah'. Testing `if z in ['yes', 'yeah']` would be better syntax. Pointing out that `if z.lower() in ['yes', 'yeah']` would probably be an even better way to test. Pointing out that indentation would be more standard if it was 4 spaces rather than 2, for example. – David Buck May 21 '20 at 10:13
  • I gave an answer to his exact question. From the looks of it he might be a beginner at programming. Me telling him those stuff would just confuse him more. There are an infinite number of ways to do this like, checking if the value exists in an array if checking for larger sets, or creating an object with those as a key and checking if it exists, and lots of validations like [...keys].map(k => k.toLowerCase()) But that would just be confusing – Mr.Cappuccino May 21 '20 at 10:23
  • 1
    My point is that you solved his immediate problem, but without explaining why his code wasn't working in the first place. I agree that loads of information at once might be confusing, but, for example, I can see someone has suggested an edit to remove the parentheses and fix the indentation so, if they're approved, they too won't have any context to explain why that has been done. I'm not suggesting that your answer doesn't work, it certainly does, I'm just suggesting that there's scope to make it a better, more useful answer to the OP. With code-only answers, that's almost always the case. – David Buck May 21 '20 at 10:27
  • But yes, I do agree, I should've at least explained what was wrong with his solution. I'll try to improve my answers next time ^_^ cheers – Mr.Cappuccino May 21 '20 at 10:29