Add basic Python calculator
This commit is contained in:
29
main.py
Normal file
29
main.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
def calculate(a, b, operation):
|
||||||
|
if operation == "+":
|
||||||
|
return a + b
|
||||||
|
if operation == "-":
|
||||||
|
return a - b
|
||||||
|
if operation == "*":
|
||||||
|
return a * b
|
||||||
|
if operation == "/":
|
||||||
|
if b == 0:
|
||||||
|
raise ValueError("Division by zero is not allowed")
|
||||||
|
return a / b
|
||||||
|
raise ValueError(f"Unsupported operation: {operation}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Simple Python Calculator")
|
||||||
|
first = float(input("Enter the first number: "))
|
||||||
|
operation = input("Choose operation (+, -, *, /): ").strip()
|
||||||
|
second = float(input("Enter the second number: "))
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = calculate(first, second, operation)
|
||||||
|
print(f"Result: {result}")
|
||||||
|
except ValueError as error:
|
||||||
|
print(f"Error: {error}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
0
requirements.txt
Normal file
0
requirements.txt
Normal file
Reference in New Issue
Block a user