0% found this document useful (0 votes)
4 views9 pages

Python Expression Evaluation Program

The document describes a Python program designed to evaluate mathematical expressions based on user-selected operators. It provides a menu of operators and uses if-else statements to perform the chosen operation, handling basic arithmetic and an expression involving multiple variables. The program also includes error handling for division by zero.

Uploaded by

nandkishore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Python Expression Evaluation Program

The document describes a Python program designed to evaluate mathematical expressions based on user-selected operators. It provides a menu of operators and uses if-else statements to perform the chosen operation, handling basic arithmetic and an expression involving multiple variables. The program also includes error handling for division by zero.

Uploaded by

nandkishore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No.

-02

Aim : Write a Program in python to evaluate the expression using operator


Software: Required Google Colab .

Theory :
Here a python program is created to evaluate the expression by using the precedence of various
type of operators.
Uses the if else statement . It presents user with a menu of a operator to choose from and asks
the users the operator they want to evaluate and the program gives the desirable output to the
user .

Code :
optr = input("Type choice (1-7 or +, -, *, /, %, **, exp): ")

# Display available operators


print("There are operators:")
print("1. +")
print("2. -")
print("3. *")
print("4. /")
print("5. %")
print("6. **")
print("7. exp (x + y * z / w)")

# Perform the chosen operation


if optr == "1" or optr == "+":
a = int(input("a is: ")) optr = input("Type choice (1-7 or +, -, *, /, %, **, exp): ")

# Display available operators


print("There are operators:")
print("1. +")
print("2. -")
print("3. *")
print("4. /")
print("5. %")
print("6. **")
print("7. exp (x + y * z / w)")

# Perform the chosen operation


if optr == "1" or optr == "+":
a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a + b)

elif optr == "2" or optr == "-":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a - b)

elif optr == "3" or optr == "*":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a * b)

elif optr == "4" or optr == "/":


a = int(input("a is: "))
b = int(input("b is: "))
if b == 0:
print("Error: Division by zero is not allowed.")
else:
print("Answer is:", a / b)

elif optr == "5" or optr == "%":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a % b)

elif optr == "6" or optr == "**":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a ** b)

elif optr == "7" or optr == "exp":


x = int(input("x is: "))
y = int(input("y is: "))
z = int(input("z is: "))
w = int(input("w is: "))
exp = x + (y * z) / w
print("Answer is: (x + y * z / w) =", exp)

else:
print("You entered a wrong operator")
optr = input("Type choice (1-7 or +, -, *, /, %, **, exp): ")

# Display available operators


print("There are operators:")
print("1. +")
print("2. -")
print("3. *")
print("4. /")
print("5. %")
print("6. **")
print("7. exp (x + y * z / w)")

# Perform the chosen operation


if optr == "1" or optr == "+":
a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a + b)
elif optr == "2" or optr == "-":
a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a - b)

elif optr == "3" or optr == "*":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a * b)

elif optr == "4" or optr == "/":


a = int(input("a is: "))
b = int(input("b is: "))
if b == 0:
print("Error: Division by zero is not allowed.")
else:
print("Answer is:", a / b)

elif optr == "5" or optr == "%":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a % b)

elif optr == "6" or optr == "**":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a ** b)

elif optr == "7" or optr == "exp":


x = int(input("x is: "))
y = int(input("y is: "))
z = int(input("z is: "))
w = int(input("w is: "))
exp = x + (y * z) / w
print("Answer is: (x + y * z / w) =", exp)

else:
print("You entered a wrong operator")

b = int(input("b is: "))


print("Answer is:", a + b)

elif optr == "2" or optr == "-":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a - b)

elif optr == "3" or optr == "*":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a * b)
elif optr == "4" or optr == "/":
a = int(input("a is: "))
b = int(input("b is: "))
if b == 0:
print("Error: Division by zero is not allowed.")
else:
print("Answer is:", a / b)

elif optr == "5" or optr == "%":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a % b)

elif optr == "6" or optr == "**":


a = int(input("a is: "))
b = int(input("b is: "))
print("Answer is:", a ** b)

elif optr == "7" or optr == "exp":


x = int(input("x is: "))
y = int(input("y is: "))
z = int(input("z is: "))
w = int(input("w is: "))
exp = x + (y * z) / w
print("Answer is: (x + y * z / w) =", exp)

else:
print("You entered a wrong operator")

Output

You might also like