1

I want to use an IF statement with a Boolean OR to evaluate a variable's value using both strings and integer classes. Here is my code for one of several different switches:

if (autoSelect_mode == 'Search' or 1):
    return {'position':1}
elif (autoSelect_mode == 'Verify' or 2):
    return {'position':2}
elif (autoSelect_mode == 'Track' or 3):
    return {'position':3}

The code runs without error, but seems to ignore this switching logic altogether and selects switch position 1 regardless of my initial values. Simulation is interrupted (presumably when all code has been executed) with a prompt to Continue, Reset or Stop Simulating. If my code is 'legal' then I would expect the simulation to keep running until I override the code with a manual switch position setting.

The reason I want to use this 'mixed class' technique is so that a config file which runs the simulation can be set up with either integers (for switch positions) or strings for the actual 'human readable' textual values of these variables. Whichever class is chosen for the config file, that class would be used when setting the initial values.

Thank you.

  • You don't need `()` around if condition block in Python... Also, the condition you wrote does not work as you thinks it does. See the answer marked as original for an explanation. – Stefano Sanfilippo Jun 19 '14 at 20:26
  • it can't be as simple as repeating the IF statement before the integer value? was going to expand here but I see an answer, so thanks for that. I will check it out. – user3757895 Jun 19 '14 at 20:27
  • You should use `if autoSelect_mode in ('Search', 1):` etc. – Stefano Sanfilippo Jun 19 '14 at 20:28
  • anything or 1 is always True. If you want to test autoSelect_mode for 'Search' or the value '1', then you need `if autoSelect_mode == 'Search' or autoSelect_mode == 1` – Chrispresso Jun 19 '14 at 20:29
  • 1
    Sorry for the duplication. Thanks for the answers. – user3757895 Jun 19 '14 at 20:35

0 Answers0