-2

Hello I'm making a random program and right now I have it asking a user if they want to add, subtract, multiply, or divide and that's going well but the if statement wont go through all the options. Whatever is in the first of the nested if statement is what the function preforms. Here's the code:

#Test2.py
import random
x = input("Hello what is the issue? ").capitalize()
if x == "Can't decide what to do":
    y = input("How many choices are there? (please type as numbers in a row) ")
    yl = list(y)
    print("Number ", random.choice(yl) ," is your choice.")
elif x == "I need help with math":
    m = input("Okay, are you adding, subtracting, multiplying, dividing, or something else? ").capitalize()
    if m == "Adding" or "Add" or "Addition":
        q = input("Okay what is the first number? ")
        w = input("What is the second number? ")
        r = input("Enter the third number: (if there isnt another number please enter 0)")
        t = input("Enter the fourth number: (if there isnt another number please enter 0) ")
        u = input("Enter the fifth number: (if there isnt another number please enter 0) ")
        m1 = int(q)+int(w)+int(r)+int(t)+int(u)
        print("The answer is: ", m1)
    elif m == "Subtracting" or "Subtract" or "Subtraction":
        i = input("Okay what is the first number? ")
        o = input("What is the second number? ")
        p = input("Enter the third number: (if there isnt another number please enter 0)  ")
        a = input("Enter the fourth number: (if there isnt another number please enter 0) ")
        s = input("Enter the fifth number: (if there isnt another number please enter 0) ")
        m2 = float(i) - float(o) - float(p) - float(a) - float(s)
        print("The answer is: ", m2)
    elif m == "Multiplying" or "Multiply" or "Multiplication":
        d = input("Okay what is the first number? ")
        f = input("What is the second number? ")
        g = input("Enter the third number: (if there isnt another number please enter 1)  ")
        h = input("Enter the fourth number: (if there isnt another number please enter 1) ")
        j = input("Enter the fifth number: (if there isnt another number please enter 1) ")
        m3 = float(d) * float(f) * float(g) * float(h) * float(j)
        print("The answer is: ", m3)
    elif m == "Dividing" or "Divide" or "Division":
        k = input("Okay what is the first number? ")
        l = input("What is the second number? ")
        z = input("Enter the third number: (if there isnt another number please enter 1)  ")
        x = input("Enter the fourth number: (if there isnt another number please enter 1) ")
        c = input("Enter the fifth number: (if there isnt another number please enter 1) ")
        m4 = float(k)%float(l)%float(z)%float(x)%float(c)
        print("The answer is: ", m4)
else:
    quit

Thank you for any and all help, have a great day!

  • 1
    Could you clarify how the code is expected to work vs what it's giving as an output now? – vnk Sep 18 '21 at 17:43
  • You are not using your `or` statements properly. Consider this statement: `if "Multiply": print("Hi")`. This statement will print `Hi` because the string `"Multiply"` is interpreted as non-zero and thus `True`. So when you have `if m == "Multiplying" or "Multiply"`, it returns `True` no matter the value of `m` because it is interpreted as `if (m=="Multiplying") or ("Multiply")`. If you have something like `if m in ["Multiply", "Multiplication", "Multiplying"]` then Python will check to see if the string `m` was one of the entries in the list. – ramzeek Sep 18 '21 at 17:50
  • A tuple (```if m in ("Multiply", "Multiplication", "Multiplying")```) would be a little more Pythonic. – sj95126 Sep 18 '21 at 18:07
  • Thank you so much I havent used Python in two years, so im a little rusty lol – Kyle Kokaliares Sep 18 '21 at 21:25

1 Answers1

1

You need to put "m ==" after every "or". Python doesn't know what to compare to, if you don't give it the variable to compare to after every "or" or "and" logic operator.

For example:

elif m == "Subtracting" or m == "Subtract" or m == "Subtraction":
iamzeid
  • 104
  • 1
  • 2
  • 18