0
test = str(raw_input("Which function would you like to use? "))



print test

one = int(raw_input("Input the first number. "))
two = int(raw_input("Input the second number. "))

if test == "Addition" or "Add" or "Adding" or "+":
    print one + two

elif test == "Subtraction" or "Subtract" or "Subtracting" or "-":
    print one - two

No matter what I do, the Addition if statement is the only thing that is run so if I put 2 and 1 it will always equal 3 even if I put a -. What should I do to fix that?

karthikr
  • 97,368
  • 26
  • 197
  • 188
deeup511
  • 11
  • 1
  • 3
  • I voted to reopen because it's not quite the same as the tagged question. This one asks how to check one variable for several possible values. The other - despite its title - seems to be how to check whether any of several variables have a given a given value. I could imagine a new programmer asking deeup511's question not getting insight from the linked answer because they're structured different, even if the end answer is the same. – Kirk Strauser Nov 10 '15 at 01:15

1 Answers1

6
if test == "Addition" or "Add" or "Adding" or "+"

is interpreted as

if (test == "Addition") or "Add" or "Adding" or "+"

which always evaluates to True as the truth value of a non-empty string is always True

So to fix, you should do:

if test in ("Addition", "Add", "Adding", "+")

You might also want to consider test.lower() so that you can take into account the various cases of the words you are testing for.

Like this:

if test.lower() in ("addition", "add", "adding", "+")
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Exactly this. Alternatively, if you want to be explicit for some reason: `if (test == 'Addition') or (test == 'Add') or (test == 'Adding') or (test == '+'):`. @karthikr, not that it matters here, but that could be `if test.lower() in {'addition', ...}` to do O(1) set membership checking instead of O(n) tuple checking. – Kirk Strauser Nov 10 '15 at 01:07
  • That works thank you. Is there a way I can make it so any combination of caps works? For example: "subTRACTION" or "sUBtracTION" – deeup511 Nov 10 '15 at 01:32