0

I am trying to make a python program where the user can input "1233+1333" or "23566*5456" then press Enter And the program automatically calculates the value And if the user inputs "54432a+2432b" it should show an error that invalid input has been given

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2

print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
select = int(input("Select operations form 1, 2, 3, 4 :"))
 
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))
 
if select == 1:
    print(number_1, "+", number_2, "=",
                    add(number_1, number_2))
 
elif select == 2:
    print(number_1, "-", number_2, "=",
                    subtract(number_1, number_2))
 
elif select == 3:
    print(number_1, "*", number_2, "=",
                    multiply(number_1, number_2))
 
elif select == 4:
    print(number_1, "/", number_2, "=",
                    divide(number_1, number_2))
else:
    print("Invalid input")
AmmarAli
  • 25
  • 5
  • Which part of the code is not working for you? Or do you need help creating a new function that can handle the mentioned format? – Navkar Jain Aug 07 '23 at 05:01
  • This code does not do what you describe. This asks for the operation and the operands separately. What your description requires is a "parser", going through the characters one at a time, either building up numbers or remembering what operation to perform. If you ONLY want to allow two-number operations, you could use a regular expression. – Tim Roberts Aug 07 '23 at 05:10

3 Answers3

2

Good job with what you have done with the code and good approach with use of functions, about the issue, i suggest some changes in this particular part of the code

number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))

you are converting the input to integer, and if the user enters a illegal input like "54432a" the code would interrupt and show an Value Error. There are multitude of ways to approach this, the best would be to use an assert statement,
but what I would suggest is a simple way to remove the type conversion and rather check for the type of value the user has entered. and set an condition to check that they are numeric and since you are using input function converts the input to string, you can convert them to integer in the next line, here is what i suggest, though there are more optimal solutions. Cheers.

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2
    
print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
select = int(input("Select operations form 1, 2, 3, 4 :"))
 
number_1 = input("Enter first number: ")
number_2 = input("Enter second number: ")

if number_1.isnumeric() == True and number_2.isnumeric() == True: 
    number_1, number_2 = int(number_1), int(number_2)
    if select == 1:
        print(number_1, "+", number_2, "=",
                        add(number_1, number_2))
    
    elif select == 2:
        print(number_1, "-", number_2, "=",
                        subtract(number_1, number_2))
    
    elif select == 3:
        print(number_1, "*", number_2, "=",
                        multiply(number_1, number_2))
    
    elif select == 4:
        print(number_1, "/", number_2, "=",
                        divide(number_1, number_2))
else:
    print("Invalid input")
apan524
  • 83
  • 5
  • 1
    As per previous comments, and as specified in the question about accepting input as "1233+1333" or "23566*5456". This would require a tad bit complex approach with regex. The solution proposed above would require you to enter the two numbers in 2 different occasion and verify if they are numeric and print invalid input if otherwise. – apan524 Aug 07 '23 at 05:22
1

Here is one way to do what you ask, using a regular expression. This ONLY handles expressions with two integer operands.

import re

pat = r'(\d+)([-+*/])(\d+)'

while True:
    expr = input('Enter expression:')
    parts = re.match(pat,expr)
    if not parts:
        print( "Your input didn't match my pattern.")
        continue
    n1 = int(parts[1])
    n2 = int(parts[3])
    op = parts[2]
    if op == '+':
        print(n1+n2)
    elif op == '-':
        print(n1-n2)
    elif op == '*':
        print(n1*n2)
    elif op == '/':
        print(n1/n2)
    break
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Once input is not int it will straightly throw an error because, in Python, the if statement executes first and then the else. Instead, you can use something like exception handling so

code correction

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2

print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
while 1:
  
    select = int(input("Select operations form 1, 2, 3, 4 :"))

    try:
        number_1 = int(input("Enter first number: "))
    except ValueError:
       print("Invalid input")
       break

    try:
        number_2 = int(input("Enter second number: "))
    except ValueError:
       print("Invalid input")
       break
     
    if select == 1:
        print(number_1, "+", number_2, "=",
                        add(number_1, number_2))
     
    elif select == 2:
        print(number_1, "-", number_2, "=",
                        subtract(number_1, number_2))
     
    elif select == 3:
        print(number_1, "*", number_2, "=",
                        multiply(number_1, number_2))
     
    elif select == 4:
        print(number_1, "/", number_2, "=",
                        divide(number_1, number_2))
    else:
        print("Invalid input")
  • This does not do what the poster asked. – Tim Roberts Aug 07 '23 at 05:11
  • His code doesn't work if the input has alphabets which are non int's ... As he has not mentioned what he wants/where he stuck at so based on his requirement edited the code – Bhargav - Retarded Skills Aug 07 '23 at 05:13
  • As I mentioned, neither your code nor his code does what his description asked. He wants to enter an expression as a single string and evaluate it. He does not want three separate inputs. – Tim Roberts Aug 07 '23 at 05:33