Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple If's?
#1
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 != '+'
Reply
#2
https://python-forum.io/Thread-Multiple-...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
Reply
#3
(Mar-17-2018, 08:35 PM)buran Wrote: https://python-forum.io/Thread-Multiple-...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


Thanks. The problem was, apparently; I tried each individual char without it being bool but forgot to change 'or' to 'and' Doh
if res != '+' and res != '-' and res != '/' and res != '^' and res != '*' and res != '%':
Reply
#4
did you check the link I shared
more pythonic would be
if res not in '+-/^*%':
Reply
#5
(Mar-18-2018, 08:02 AM)buran Wrote: did you check the link I shared
more pythonic would be
if res not in '+-/^*%':

This would be the most appropriate solution for this problem. The code is clean
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020