-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Gc #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gc #1994
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
print("3.Multiply") | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a IDE if possible while coding. |
||
|
||
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") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understand these snippet.