Skip to content
Open
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
36 changes: 36 additions & 0 deletions Calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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


print("=== Simple Calculator ===")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

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

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
Comment on lines +24 to +25
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

No input validation for numeric conversion.

float() will raise ValueError if the user enters non-numeric input, crashing the program. Wrap in try/except for a better user experience.

🛠️ Add input validation
-num1 = float(input("Enter first number: "))
-num2 = float(input("Enter second number: "))
+try:
+    num1 = float(input("Enter first number: "))
+    num2 = float(input("Enter second number: "))
+except ValueError:
+    print("Invalid input. Please enter numeric values.")
+    exit(1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter numeric values.")
exit(1)
🤖 Prompt for AI Agents
In `@Calculator.py` around lines 24 - 25, The conversions to float for num1 and
num2 lack validation and will raise ValueError on non-numeric input; change the
logic so you repeatedly prompt until a valid float is entered (e.g., implement a
helper like get_float_input(prompt) that wraps float(input(...)) in a try/except
ValueError loop and returns a float), and replace the direct assignments to num1
and num2 with calls to that helper so invalid input is handled gracefully
instead of crashing.


if choice == "1":
print("Result:", add(num1, num2))
elif choice == "2":
print("Result:", subtract(num1, num2))
elif choice == "3":
print("Result:", multiply(num1, num2))
elif choice == "4":
print("Result:", divide(num1, num2))
else:
print("Invalid choice")
Comment on lines +16 to +36
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Missing if __name__ == "__main__": guard.

The CLI code executes immediately when the module is loaded. This prevents reusing the functions via import Calculator without triggering the interactive prompts.

🛠️ Wrap CLI in main guard
+def main():
+    print("=== Simple Calculator ===")
+    print("1. Add")
+    print("2. Subtract")
+    print("3. Multiply")
+    print("4. Divide")
+
+    choice = input("Enter choice (1/2/3/4): ")
+
+    num1 = float(input("Enter first number: "))
+    num2 = float(input("Enter second number: "))
+
+    if choice == "1":
+        print("Result:", add(num1, num2))
+    elif choice == "2":
+        print("Result:", subtract(num1, num2))
+    elif choice == "3":
+        print("Result:", multiply(num1, num2))
+    elif choice == "4":
+        print("Result:", divide(num1, num2))
+    else:
+        print("Invalid choice")
+
+if __name__ == "__main__":
+    main()
-print("=== Simple Calculator ===")
-print("1. Add")
-print("2. Subtract")
-print("3. Multiply")
-print("4. Divide")
-
-choice = input("Enter choice (1/2/3/4): ")
-
-num1 = float(input("Enter first number: "))
-num2 = float(input("Enter second number: "))
-
-if choice == "1":
-    print("Result:", add(num1, num2))
-elif choice == "2":
-    print("Result:", subtract(num1, num2))
-elif choice == "3":
-    print("Result:", multiply(num1, num2))
-elif choice == "4":
-    print("Result:", divide(num1, num2))
-else:
-    print("Invalid choice")
🤖 Prompt for AI Agents
In `@Calculator.py` around lines 16 - 36, The CLI block runs at import time; wrap
the interactive prompt and choice-handling logic inside a main guard by moving
the printed menu, input() calls, and the if/elif chain that calls add, subtract,
multiply, divide into a function (e.g., main()) and call that function only
under if __name__ == "__main__": so importing Calculator won't execute the
prompts.