이 프로그램에서 우리는 파이썬 프로그램을 사용하여 계산기의 기본 계산기 기능을 수행하는 방법을 볼 것입니다. 여기에서 계산을 수행하고 결과를 반환하는 개별 함수를 만듭니다. 또한 운영자 선택과 함께 사용자 입력이 허용됩니다.
예시
# This function performs additiion
def add(a, b):
return a + b
# This function performs subtraction
def subtract(a, b):
return a - b
# This function performs multiplication
def multiply(a, b):
return a * b
# This function performs division
def divide(a, b):
return a / b
print("Select an operation.")
print("+")
print("-")
print("*")
print("/")
# User input
choice = input("Enter operator to use:")
A = int(input("Enter first number: "))
B = int(input("Enter second number: "))
if choice == '+':
print(A,"+",B,"=", add(A,B))
elif choice == '-':
print(A,"-",B,"=", subtract(A,B))
elif choice == '*':
print(A,"*",B,"=", multiply(A,B))
elif choice == '/':
print(A,"/",B,"=", divide(A,B))
else:
print("Invalid input") 출력
위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -
Select an operation. + - * / Enter operator to use: - Enter first number: 34 Enter second number: 20 34 - 20 = 14