0

Just started learning python and I have been doing pet projects. During this one I decided to use the OR operator. When I added it to one of my IF STATEMENTS it worked as intended but when I added it to the other 3 it only ran the first IF STATEMENT.


while True:
    
    operation = input("What order of operation would you like to use(Addition/A ,Subtraction/S ,Multiplication/M, Division/D)?")
    
    if operation == "Addition" or "a" :
        while True:
            Set_Number = int(input("Starting Number: "))
            counter = Set_Number
            print(counter)
            
            number = int(input("Add:"))
            add_count = number
            counter += add_count
            print(f"Your Answer is " + str(counter))
            
    if operation == "Multiplication" or "m" :
        while True: 
            Set_Number = int(input("Starting Number: "))
            counter = Set_Number
            print(counter)
            
            number = int(input("Multipled By:"))
            add_count = number
            counter = counter * add_count
            print(f"Your Answer is " + str(counter))
        
    if operation == "Division" or "d" :
        while True:
            Set_Number = int(input("Starting Number: "))
            counter = Set_Number
            print(counter)
            
            number = int(input("Divided By:"))
            add_count = number
            counter = counter/add_count
            print(f"Your Answer is " + str(counter))
                    
    if operation == "Subtraction" or "s" :
        while True:
            Set_Number = int(input("Starting Number: "))
            counter = Set_Number
            print(counter)
            
            number = int(input("Subtracted By:"))
            add_count = number
            counter = counter - add_count
            print(f"Your Answer is " + str(counter))

Result

What order of operation would you like to use(Addition/A ,Subtraction/S ,Multiplication/M, Division/D)?m
Starting Number: 8
8
Add:1
Your Answer is 9
Starting Number:

Don't know how to fix. Need Help.

Beginner
  • 31
  • 6
  • 3
    `==` has higher precedence than `or` so (based on what you have) you may want to use want to do something like this: `operation in ("Addition" or "a")` or `(operation == "Addition") or (operation = "a")`. – David Lee Jul 08 '21 at 15:03
  • see https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true – zr0gravity7 Jul 08 '21 at 15:03
  • @David `a in (b or c)` doesn't do what you think it does either, and you don't need parentheses for `a == b or a == c`. *Precedence* isn't the issue. – deceze Jul 08 '21 at 15:05
  • You may use in the other if-Statements ‘elif‘ – lkrss Jul 08 '21 at 15:05
  • Note that basically the entire block inside each `if` is virtually identical except for the actual mathematical operation. You can make that a lot less repetitive and shorter by only putting that one line that differs into an `if..else` instead of the entire thing. – deceze Jul 08 '21 at 15:12
  • @deceze O I see thanks – Beginner Jul 08 '21 at 15:18

0 Answers0