[go: up one dir, main page]

0% found this document useful (0 votes)
14 views2 pages

Enhanced Calculator Script

This document contains a Python script for an advanced calculator that supports various mathematical operations including addition, subtraction, multiplication, division, modulus, exponentiation, square root, and floor division. The script runs in a loop, allowing users to select operations until they choose to exit. Error handling is included for division by zero and square root of negative numbers.

Uploaded by

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

Enhanced Calculator Script

This document contains a Python script for an advanced calculator that supports various mathematical operations including addition, subtraction, multiplication, division, modulus, exponentiation, square root, and floor division. The script runs in a loop, allowing users to select operations until they choose to exit. Error handling is included for division by zero and square root of negative numbers.

Uploaded by

staffdewa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Calculator Script (Python)

This script supports the following operations:


1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
6. Exponentiation (x^y)
7. Square Root
8. Floor Division
9. Exit

Python Code:

import math

def calculator():
while True:
print("\nAdvanced Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiation (x^y)")
print("7. Square Root")
print("8. Floor Division")
print("9. Exit")

choice = input("Enter choice (1-9): ")

if choice == '9':
print("Exiting calculator. Goodbye!")
break

if choice in ['1', '2', '3', '4', '5', '6', '8']:


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

if choice == '1':
print(f"{num1} + {num2} = {num1 + num2}")
elif choice == '2':
print(f"{num1} - {num2} = {num1 - num2}")
elif choice == '3':
print(f"{num1} * {num2} = {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"{num1} / {num2} = {num1 / num2}")
else:
print("Error: Division by zero.")
elif choice == '5':
print(f"{num1} % {num2} = {num1 % num2}")
elif choice == '6':
print(f"{num1} ^ {num2} = {num1 ** num2}")
elif choice == '8':
print(f"{num1} // {num2} = {num1 // num2}")

elif choice == '7':


num = float(input("Enter number: "))
if num >= 0:
print(f"·{num} = {math.sqrt(num)}")
else:
print("Error: Cannot compute square root of a negative number.")

else:
print("Invalid input. Please select a valid operation.")

# Run the calculator


calculator()

You might also like