calculator / app.py
isana25's picture
Update app.py
f1b98e6 verified
import gradio as gr
def calculate(num1, operation, num2):
try:
if operation == "Addition (+)":
result = num1 + num2
elif operation == "Subtraction (-)":
result = num1 - num2
elif operation == "Multiplication (×)":
result = num1 * num2
elif operation == "Division (÷)":
result = num1 / num2
elif operation == "Modulus (%)":
result = num1 % num2
elif operation == "Power (^)":
result = num1 ** num2
elif operation == "Floor Division (//)":
result = num1 // num2
else:
return "Invalid operation selected."
return f"Result: {result}"
except Exception as e:
return f"Error: {e}"
with gr.Blocks() as demo:
gr.Markdown("## 🧮 Simple Calculator")
with gr.Row():
num1 = gr.Number(label="First Number")
operation = gr.Dropdown(
["Addition (+)", "Subtraction (-)", "Multiplication (×)", "Division (÷)",
"Modulus (%)", "Power (^)", "Floor Division (//)"],
label="Operation"
)
num2 = gr.Number(label="Second Number")
calculate_button = gr.Button("Calculate")
result_output = gr.Textbox(label="Result")
calculate_button.click(fn=calculate, inputs=[num1, operation, num2], outputs=result_output)
demo.launch()