Skip to content
Merged

Gc #1994

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 72 additions & 23 deletions Calculator with simple ui.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
# Program make a simple calculator

# This function adds two numbers
def add(x, y):
return x + y

# This function subtracts two numbers
def subtract(x, y):
return x - y
class Calculator:
def __init__(self):
pass

# This function multiplies two numbers
def multiply(x, y):
return x * y
def add(self, num1, num2):
"""
This function adds two numbers.

# This function divides two numbers
def divide(x, y):
return x / y
Examples:
>>> add(2, 3)
5
>>> add(5, 9)
14
>>> add(-1, 2)
1
"""
return num1 + num2

def subtract(self, num1, num2):
"""
This function subtracts two numbers.

Examples:
>>> subtract(5, 3)
2
>>> subtract(9, 5)
4
>>> subtract(4, 9)
-5
"""
return num1 - num2

def multiply(self, num1, num2):
"""
This function multiplies two numbers.

Examples:
>>> multiply(4, 2)
8
>>> multiply(3, 3)
9
>>> multiply(9, 9)
81
"""
return num1 * num2

def divide(self, num1, num2):
"""
This function divides two numbers.

Examples:
>>> divide(4, 4)
1
>>> divide(6, 3)
2
>>> divide(9, 1)
9
"""
if num2 == 0:
print("Cannot divide by zero")
else:
return num1 / num2


calculator = Calculator()


print("Select operation.")
print("1.Add")
print("2.Subtract")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calculator = Calculator()

# A dictionary that maps the choices to the functions
operations = {
    "1": calculator.add,
    "2": calculator.subtract,
    "3": calculator.multiply,
    "4": calculator.divide
}

# A loop that asks the user for input until they enter a valid choice
while True:
    print("Select operation.")
    print("1.Add")
    print("2.Subtract")
    print("3.Multiply")
    print("4.Divide")

    choice = input("Enter choice(1/2/3/4): ")

    # Check if the choice is valid
    if choice in operations:
        
        # Ask the user for two numbers
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

        # Get the function by its key from the dictionary
        operation = operations.get(choice)

        # Call the function with the two numbers and print the result
        result = operation(num1, num2)
        print(f"The result is {result}")

        # Break out of the loop
        break
    else:
        # Print an error message and continue the loop
        print("Invalid input. Please try again.")

Understand these snippet.

print("3.Multiply")
Expand All @@ -28,22 +78,21 @@ def divide(x, y):
choice = input("Enter choice(1/2/3/4): ")

# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
if choice in ("1", "2", "3", "4"):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
if choice == "1":
print(calculator.add(num1, num2))

elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == "2":
print(calculator.subtract(num1, num2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a IDE if possible while coding.
Also, Use a code formatter if possible.
If possible, enable code formatter when saving the file.
You can also format the code by black.
to install just type in cmd pip install black
then format the file using cmd by typing in cmd black file_name.py
and worry not, there are many ways to format a file. This clicks with me, that's it.


elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == "3":
print(calculator.multiply(num1, num2))

elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == "4":
print(calculator.divide(num1, num2))
break
else:
print("Invalid Input")