VisuAlg Web

Operações Matemáticas Básicas

Nesta versão Python, o exemplo integra leitura de menu, conversão com float() e tratamento de decisão com if/elif/else .

Nesta versão Python, o exemplo integra leitura de menu, conversão com float() e tratamento de decisão com if/elif/else.

Exemplo de códigoPython


print("=== SIMPLE CALCULATOR ===")
print("[1] Add | [2] Subtract | [3] Multiply | [4] Divide")
operation = input()

print("Enter the first value: ", end="")
num1 = float(input())
print("Enter the second value: ", end="")
num2 = float(input())

if operation == "1":
result = num1 + num2
print("Result: ", result)
elif operation == "2":
result = num1 - num2
print("Result: ", result)
elif operation == "3":
result = num1 * num2
print("Result: ", result)
elif operation == "4":
if num2 == 0:
print("ERROR: Division by zero is not allowed")
else:
result = num1 / num2
print("Result: ", result)
else:
print("ERROR: Invalid operator")
Exemplo de códigoPython
import os

while True:
os.system("cls" if os.name == "nt" else "clear")
print("=== INFINITE CALCULATOR ===")
print("[1] Add | [2] Subtract | [0] EXIT PROGRAM")
operation = input("Choose an option: ")

if operation == "0":
break

if operation in {"1", "2"}:
num1 = float(input("First value: "))
num2 = float(input("Second value: "))

if operation == "1":
result = num1 + num2
else:
result = num1 - num2

print("Result:", result)
else:
print("Invalid option! Use 1, 2, or 0.")

input("Press Enter to continue...")

print("System finished. Goodbye!")