-1

I am trying to create a basic program in python, where I perform calculations based on sentences. For that I have created if/else conditions but after running the program it is taking the first condition every time, do you have any idea why it is happening?

import re
query = 'plus 5 and 3'

def multiply(query):
    query = '*'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def addition(query):
    query = '+'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def subtract(query):
    query = '-'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def divide(query):
    query = '/'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'  
    
    
    

if 'multiplication' or 'multiply' in query:
    print(multiply(query))

elif 'addition' or 'add' or 'plus' in query:
    print(addition(query))

elif 'subtraction' or 'subtract' or 'minus' in query:
    print(subtract(query))

elif 'division' or 'divide' in query:
    print(divide(query))  
Ibrennan208
  • 1,345
  • 3
  • 14
  • 31
Lokesh Thakur
  • 138
  • 1
  • 1
  • 9

2 Answers2

2

You cannot say if 'multiplication' or 'multiply' in query. If you want to check if one of those two words is in query, then you must do it like this: if 'multiplication' in query or 'multiply' in query. You will need to change this for all 4 if statements.

linger1109
  • 500
  • 2
  • 11
0

you are checking for if 'multiplication' or if 'multiply' in query. what you want to check is if 'multiplication' in query or if 'multiply' in query

the correct code would be:

import re
query = 'plus 5 and 3'

def multiply(query):
    query = '*'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def addition(query):
    query = '+'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def subtract(query):
    query = '-'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'

def divide(query):
    query = '/'.join(re.findall('\d+',query))
    print(f'{eval(query)}')
    return f'Answer is {eval(query)}'  
    
    
    

if 'multiplication' in query or 'multiply' in query:
    print(multiply(query))

elif 'addition' in query or 'add' in query or 'plus' in query:
    print(addition(query))

elif 'subtraction' in query or 'subtract' in query or 'minus' in query:
    print(subtract(query))

elif 'division' in query or 'divide' in query:
    print(divide(query))  
LaPeSi
  • 99
  • 11