PYTHON Write a GUI based program that simulates a simple pocket calculator. The
ID: 3717078 • Letter: P
Question
PYTHON
Write a GUI based program that simulates a simple pocket calculator. The GUI displays a single entry field for output (I would suggest to make it a read only field). It should present the user with 10 numeric buttons from 0 to 9 and seven function keys for +, -, *, /, C (C is for clearing the display), . (decimal point) and =. = is to calculate the correct answer. If there is an error like divide by zero the entry field should display ERR or Error. Values in the entry field will build a string that will be converted to a float for calculation.
Explanation / Answer
Program to perform Basic Caluclator
import sys
''' Program make a simple calculator that can add, subtract, multiply and divide using functions '''
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = int(input("Enter choice(1/2/3/4):"))
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == 1:
print(num1,"+",num2,"=", add(num1,num2))
elif choice == 2:
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == 3:
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == 4:
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("invalid")
output
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):1
Enter first number: 5
Enter second number: 5
5 + 5 = 10
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):2
Enter first number: 5
Enter second number: 4
5 - 4 = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.