0

Issue

If-statements are letting a condition through even though it doesn't fit the parameter

Description

This piece of code is supposed to run when the selection is equal to either index 2 of the list operations or index 3 of the list. Yet when selection is equal to index 1 of the list operations the code still runs

if v.selection == v.operations[2] or v.operations[3]:
    print('arrived at a sub n missing - geometric')
    print('index of ', v.selection, 'in the list is ', v.operations.index(v.selection))
    if not v.aSub1:
        v.aSub1 = input('first value in sequence>>>')
    if not v.r:
        v.r = input('common rate>>>')
    if not v.n:
        v.n = input('index (n)>>>')
        pass
    pass
pass

output

  • It is better to put the output here instead of image. Makes it easy for people to help – Umang Gupta Dec 16 '17 at 02:01
  • @RayToal thanks, clear and to the point –  Dec 16 '17 at 03:01
  • @RayToal one thing, is there a performance difference between using `{v.operations.....}` and `(v.operations.....)` ? –  Dec 16 '17 at 03:08
  • It's probably not significant between a set of two elements versus a tuple of two elements would have a negligible difference. You can always just say `v.selection == v.operations[2] or v.selection == v.operations[3]` if you were worried about performance, but it's generally not worth it IMHO. – Ray Toal Dec 16 '17 at 03:38

1 Answers1

1

You're looking at the if statement from the wrong angle. What you wrote evaluates to: either v.selection equals v.operations[2] OR v.operations[3] is not false. for that matter, anything that isn't 0 is not false. Here's an example:

a = 5;
b = 6;
c = 7;
if (a == b or c):
    print("hey")
else:
    print("nay")

which results in:

hey
oBit91
  • 398
  • 1
  • 12