I created a small calculator game in Python that can perform simple operations like addition, subtraction, multiplication, and division.
The goal is to learn while having fun, and to explore the basics of Python programming.
This project is perfect for beginners who want to practice logic and user interaction.
Here is the Python code for the calculator:
Simple Calculator Game in Python
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error: division by zero"
return a / b
def get_number(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number.")
def main():
print("=== Simple Calculator ===")
while True:
print("\nChoose an operation:")
print("1) Add")
print("2) Subtract")
print("3) Multiply")
print("4) Divide")
print("5) Exit")
choice = input("Your choice (1-5): ").strip()
if choice == "5":
print("Goodbye!")
break
if choice not in {"1", "2", "3", "4"}:
print("Invalid choice. Try again.")
continue
a = get_number("Enter the first number: ")
b = get_number("Enter the second number: ")
if choice == "1":
result = add(a, b)
op = "+"
elif choice == "2":
result = subtract(a, b)
op = "-"
elif choice == "3":
result = multiply(a, b)
op = "*"
else: # choice == "4"
result = divide(a, b)
op = "/"
print(f"Result: {a} {op} {b} = {result}")
if name == "main":
main()
Conclusion:
This simple Python calculator game is a great way for beginners to practice programming basics and have fun experimenting with code. Feel free to modify it and add new features!__