So I was making my own terminal with a list of commands and a series of if-statements to check them. If the command does not exist, it returns an error. If the input is contained in the list of commands, regardless of which command, it will execute only the first if-statement. E.g. I enter 3 and it will still return '1 or 2'. This has to do with the 'or' statement, but I'm not sure why. If I use if-statements with only one variable the code runs fine, but I'd like to check for multiple commands:
- For efficiency
- So multiple spellings of a command can be excepted. e.g. '1' and 'one'.
I know this is caused by the or function, but I can't see why. Anyone have any ideas?
Below is an example code, simplified, but produces the same results as my actual script:
commands = ['1', '2', '3', '4']
if command in commands:
if command == '1' or '2':
print('1 or 2')
elif command == '3' or '4':
print('3 or 4')
else:
print('UNDEFINED COMMAND')
else:
print("INVALID COMMAND")```