-
Notifications
You must be signed in to change notification settings - Fork 503
Adding calculator app #21
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
base: master
Are you sure you want to change the base?
Adding calculator app #21
Conversation
📝 WalkthroughWalkthroughA new Calculator.py module is introduced containing four arithmetic functions (add, subtract, multiply, divide) with division-by-zero protection, paired with an interactive command-line interface that prompts users for operation selection and operands. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@Calculator.py`:
- Around line 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.
- Around line 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.
🧹 Nitpick comments (1)
Calculator.py (1)
10-13: Mixed return type individe()is problematic.The function returns either a
floator astr, which can cause issues for callers who expect a numeric result. Consider raising aValueErrororZeroDivisionErrorinstead, or returningNonefor error cases.♻️ Suggested fix: raise an exception for consistency
def divide(a, b): if b == 0: - return "Error: Division by zero" + raise ZeroDivisionError("Cannot divide by zero") return a / bThen update the caller to handle the exception:
elif choice == "4": try: print("Result:", divide(num1, num2)) except ZeroDivisionError as e: print("Error:", e)
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
Calculator.py
🔇 Additional comments (1)
Calculator.py (1)
1-8: LGTM!The
add,subtract, andmultiplyfunctions are clean and correct implementations.
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
| 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") No newline at end of file |
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.
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.
| num1 = float(input("Enter first number: ")) | ||
| num2 = float(input("Enter second number: ")) |
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.
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.
| 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.
Added a new calculator app in python
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.