- The program needs to determine whether a given number is even or odd.
- Even numbers are divisible by 2 with no remainder (e.g., 2, 4, 6, etc.).
- Odd numbers have a remainder of 1 when divided by 2 (e.g., 1, 3, 5, etc.).
- Get input from the user.
- Convert the input to an integer.
- Use the modulus operator (
%) to check the remainder when the number is divided by 2. - If the remainder is 0, the number is even; otherwise, it's odd.
- Display the result to the user.
# Step 1: Get input from the user
number = int(input("Enter a number: "))
# Step 2: Check if the number is even or odd
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")- What if the user enters a non-numeric input? We might want to add input validation.
- What if the user enters a very large number? We need to ensure that the program handles large integers.
try:
# Step 1: Get input from the user
number = int(input("Enter a number: "))
# Step 2: Check if the number is even or odd
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
except ValueError:
print("Please enter a valid integer.")The code we've written is functional and handles basic input validation, but there are some potential areas for improvement:
- Issue: When the user enters an invalid integer, they only receive the message "Please enter a valid integer."
- Improvement: We could provide more context or allow the user to retry without restarting the program.
- Issue: The code does not differentiate between positive and negative numbers.
- Improvement: If the context requires distinguishing between negative and positive numbers, we need to consider adding a check.
- Issue: The code assumes that the input will always be numeric. If the user enters special characters or large numbers, it may not behave as expected.
- Improvement: We could add further validation to handle such cases more gracefully.
- Issue: The program terminates after one input, which might be inconvenient if the user wants to check multiple numbers.
- Improvement: We could add a loop to allow multiple inputs or ask the user if they want to check another number.
- Issue: The program is currently very simple and specific. If you wanted to extend it to perform other types of input checks or operations, it would require significant restructuring.
- Improvement: We should consider structuring the code in a more modular way, possibly using functions to separate the logic.
- Issue: There’s no logging, which can make debugging difficult if the program is part of a larger application.
- Improvement: We can add logging to track the inputs and outputs, which would be useful for debugging or auditing.
- Issue: The code has no automated tests to verify its functionality.
- Improvement: We can implement unit tests to ensure that the code works correctly across different scenarios.
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def check_number(number):
"""
Function to check if a number is even or odd and log the result.
"""
if number % 2 == 0:
logging.info(f"{number} is an even number.")
return f"{number} is an even number."
else:
logging.info(f"{number} is an odd number.")
return f"{number} is an odd number."
def main():
while True:
try:
# Step 1: Get input from the user
user_input = input("Enter a number (or type 'exit' to quit): ")
# Exit condition
if user_input.lower() == 'exit':
print("Goodbye!")
break
# Convert input to an integer
number = int(user_input)
# Step 2: Check if the number is even or odd
result = check_number(number)
print(result)
except ValueError:
logging.warning("Invalid input. Please enter a valid integer.")
print("Please enter a valid integer.")
# Run the main function
if __name__ == "__main__":
main()-
Enhanced User Feedback:
- Added a logging mechanism to track user inputs and results. When the user enters an invalid integer, the program logs the event and provides feedback.
-
Handling Negative Numbers:
- The code can handle negative numbers as even or odd without additional changes. However, if you want to explicitly differentiate positive and negative numbers, that logic can be added.
-
Handling Special Characters and Large Numbers:
- Added a
try-exceptblock to catch non-numeric inputs and provide feedback, ensuring the program doesn’t crash.
- Added a
-
Looping for Continuous Input:
- Introduced a loop to allow the user to check multiple numbers without restarting the program. The user can exit the loop by typing "exit".
-
Modular Code Structure:
- The logic to check if a number is even or odd has been separated into a function (
check_number). This makes the code more modular and easier to extend.
- The logic to check if a number is even or odd has been separated into a function (
-
Logging:
- Added logging to record inputs and results, which is useful for debugging and auditing.
-
Unit Tests:
- To add unit tests, you could use a framework like
unittestorpytest. This wasn’t included in the script, but here’s an example of how you might write a simple unit test for thecheck_numberfunction:
- To add unit tests, you could use a framework like
import unittest
class TestNumberCheck(unittest.TestCase):
def test_even_number(self):
self.assertEqual(check_number(2), "2 is an even number.")
def test_odd_number(self):
self.assertEqual(check_number(3), "3 is an odd number.")
def test_negative_even_number(self):
self.assertEqual(check_number(-4), "-4 is an even number.")
def test_negative_odd_number(self):
self.assertEqual(check_number(-5), "-5 is an odd number.")
if __name__ == '__main__':
unittest.main()This structure should make the code more robust, user-friendly, and scalable for future enhancements.