Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit 6b28789

Browse files
simple_calculator
1 parent ca1a83c commit 6b28789

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎simple_calculator‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
1+
# Define a function to add two numbers
12
def add(x, y):
23
"""Function to add two numbers"""
34
return x + y
45

6+
# Define a function to subtract two numbers
57
def subtract(x, y):
68
"""Function to subtract two numbers"""
79
return x - y
810

11+
# Define a function to multiply two numbers
912
def multiply(x, y):
1013
"""Function to multiply two numbers"""
1114
return x * y
1215

16+
# Define a function to divide two numbers
1317
def divide(x, y):
1418
"""Function to divide two numbers"""
19+
# Check if the second number is not zero to avoid division by zero error
1520
if y != 0:
1621
return x / y
1722
else:
23+
# Return an error message if the second number is zero
1824
return "Error: Cannot divide by zero"
1925

26+
# Define the main function that serves as the entry point of the program
2027
def main():
28+
# Display the title of the program
2129
print("Simple Calculator")
30+
# Display the available mathematical operations
2231
print("Available operations:")
2332
print("1. Add")
2433
print("2. Subtract")
2534
print("3. Multiply")
2635
print("4. Divide")
2736

37+
# Prompt the user to select an operation
2838
choice = input("Enter your choice (1/2/3/4): ")
2939

40+
# Prompt the user to enter the first number
3041
num1 = float(input("Enter the first number: "))
42+
# Prompt the user to enter the second number
3143
num2 = float(input("Enter the second number: "))
3244

45+
# Perform the selected operation and display the result
3346
if choice == '1':
3447
print(f"Result: {num1} + {num2} = {add(num1, num2)}")
3548
elif choice == '2':
@@ -39,7 +52,10 @@ def main():
3952
elif choice == '4':
4053
print(f"Result: {num1} / {num2} = {divide(num1, num2)}")
4154
else:
55+
# Inform the user if an invalid choice was made
4256
print("Invalid choice. Please select a valid operation (1/2/3/4).")
4357

58+
# Check if the script is being run directly and not imported
4459
if __name__ == "__main__":
60+
# If the script is run directly, call the main function to start the calculator
4561
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /