[go: up one dir, main page]

0% found this document useful (0 votes)
23 views1 page

Basic Calculator

This document describes a basic calculator script that allows users to perform addition, subtraction, multiplication, and division on two numbers. The program prompts for user input to select an operation and enter the numbers, then executes the chosen calculation while handling division by zero errors. It provides feedback for invalid inputs as well.

Uploaded by

akashtcs2
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)
23 views1 page

Basic Calculator

This document describes a basic calculator script that allows users to perform addition, subtraction, multiplication, and division on two numbers. The program prompts for user input to select an operation and enter the numbers, then executes the chosen calculation while handling division by zero errors. It provides feedback for invalid inputs as well.

Uploaded by

akashtcs2
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
You are on page 1/ 1

Basic_Calculator

Code:
def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = input("Enter choice (1/2/3/4): ")

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

if choice == '1':
print(f"Result: {num1 + num2}")
elif choice == '2':
print(f"Result: {num1 - num2}")
elif choice == '3':
print(f"Result: {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"Result: {num1 / num2}")
else:
print("Cannot divide by zero.")
else:
print("Invalid input")

calculator()

Explanation:
This script implements a basic calculator that allows the user to add, subtract, multiply, or
divide two numbers.
The program prompts the user to select an operation, enter two numbers, and then
performs the corresponding calculation.
It also handles division by zero errors gracefully.

You might also like