multiple If's? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: multiple If's? (/thread-9021.html) |
multiple If's? - Kuron3ko - Mar-17-2018 So I'm having trouble with a chunk of code that is supposed to check multiple characters against a user input. The problem is, it will only work for the first character. If I try to add more options with the 'or' the whole thing basically stops working. This is how I remember 'or' working and I don't know why it won't run here. res = str(input('What calculation would you like to do?[+] [-] [/] [*] [%] [^]: ')) if res != '+' or '-' or '/' or '^' or '*' or '%': print("please choose ", arr) res = str(input('Enter: '))Can anybody assist? It prints the next input request no matter what. I tried res != each individual char and that didn't help either. It works fine if it's only res != '+' RE: multiple If's? - buran - Mar-17-2018 https://python-forum.io/Thread-Multiple-expressions-with-or-keyword there are other approaches too (i.e. not multiple conditions with or )also if you want to exclude these chars, you need and , not or
RE: multiple If's? - Kuron3ko - Mar-17-2018 (Mar-17-2018, 08:35 PM)buran Wrote: https://python-forum.io/Thread-Multiple-expressions-with-or-keyword Thanks. The problem was, apparently; I tried each individual char without it being bool but forgot to change 'or' to 'and' if res != '+' and res != '-' and res != '/' and res != '^' and res != '*' and res != '%': RE: multiple If's? - buran - Mar-18-2018 did you check the link I shared more pythonic would be if res not in '+-/^*%': RE: multiple If's? - zero - Mar-18-2018 (Mar-18-2018, 08:02 AM)buran Wrote: did you check the link I shared This would be the most appropriate solution for this problem. The code is clean |