0

Here is a excerpt from my program:

weaponinput = input("Would you like a rifle, a pistol, or a shotgun?")
if weaponinput == " pistol":
    weapon = (int(pistol_1))
if weaponinput == " rifle":
    weapon = (int(rifle_1))
if weaponinput == " shotgun":
    weapon = (int(shotgun_1))
if weaponinput != (" shotgun") or (" rifle") or (" pistol") or (" sniper rifle"):
    print("In your futile attempt to turn",weaponinput,"into a weapon you accidentally blow your brains accross the ground.")

The if clause always triggers in line 8, no matter the value of weaponinput. Why does this happen? I am using python, and do not really understand many other languages

Anonymoose
  • 19
  • 6
  • 1
    You can't speak to a computer in human sentences. `weaponinput != "shotgun" or "rifle"` is equivalent to `(weaponinput != "shotgun") or "rifle"`. `"rifle"` is a nonempty collection, and evaluates as true. – Hammerite May 08 '17 at 20:28
  • 1
    Are you actually typing a leading space when entering a value? You're checking for one, for some reason... – jasonharper May 08 '17 at 20:28
  • 1
    Possible duplicate of [Why non-equality check of one variable against many values always returns true?](http://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Barmar May 08 '17 at 21:25
  • @jasonharper yes I am – Anonymoose May 08 '17 at 23:31

5 Answers5

4

You have written the equivalent of

if (w != 1) or (2) or (3):
 print("something")

(2) is non-zero and therefore True. In your code ("rifle") is not None and therefore True.

The correct form is

if (w != 1) or (w!=2) or (w!=3):
    ...

Another way to do this might be

if weaponinput == "rifle:
    ...
elif weaponinput == "pistol": 
     ...
else:
    print("bad input message")

Yet another way:

WeaponCodes = {"pistol":int(pistol1), "rifle":int(rifle1), ... }
try:
   weapon = WeaponCodes[weaponinput]
except KeyError:
   print("bad input message")
AShelly
  • 34,686
  • 15
  • 91
  • 152
2

you need to change that line to the following:

if weaponinput != " shotgun" or weaponinput != " rifle" or weaponinput != " pistol" or weaponinput != " sniper rifle":
Meccano
  • 537
  • 4
  • 8
1

Pythonic would be:

if weaponinput not in (" shotgun", " rifle", " pistol", " sniper rifle"):
    print(...)
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

To check multiple conditions you have to compare your input variable with the required condition like:

if var <conditional operator1> condition1 <logical operator> var <conditional operator2> condition2 and so on.

so in your case line number 8 must be: if weaponinput != " shotgun" or weaponinput != " rifle" or weaponinput != " pistol" or weaponinput != " sniper rifle":

Gaurav Paliwal
  • 1,556
  • 16
  • 27
0

Out of all operators, the logical operators(or, and and not) has the least priority (Python Operator Precedence).

In Python the Boolean value of a non-empty string is always true.

So, the code you have written is equivalent to :-

weaponinput = input("Would you like a rifle, a pistol, or a shotgun?")
if weaponinput == " pistol":
    weapon = (int(pistol_1))
if weaponinput == " rifle":
    weapon = (int(rifle_1))
if weaponinput == " shotgun":
    weapon = (int(shotgun_1))
if weaponinput != (" shotgun") or True or True or True:
    print("In your futile attempt to turn",weaponinput,"into a weapon you accidentally blow your brains accross the ground.")

So, the condition in line 8 is always true irrespective of whether weaponinput is equal to " shotgun" or not(as or always evaluates to true if atleast one of its operand is true).