Basic while loop with user input

 Basic while loop with user input.   Write an expression that executes the loop body as long as the user enters a non-negative number. Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report “Program end never reached.” The system doesn’t print the test case that caused the reported message. Sample outputs with inputs: 9 5 2 -1 Body Body Body Done.   Use Python, please. CHALLENGE ACTIVITY 5.2.2: Basic while loop with user input.   Write an expression that executes the loop body as long as the user enters a non-negative number.

Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report “Program end never reached.” The system doesn’t print the test case that caused the reported message.

Sample outputs with inputs: 9 5 2 -1 Body Body Body Done.   Use Python, please.

The Correct Answer and Explanation is :

To create a Python program that repeatedly prompts the user for input and executes a specific action as long as the user enters a non-negative number, you can utilize a while loop. The loop will continue to run until a negative number is entered, at which point it will terminate and print a concluding message. Here’s how you can implement this:

while True:
    user_input = int(input())
    if user_input < 0:
        break
    print("Body")
print("Done.")

Explanation:

  1. Infinite Loop Initialization: while True: starts an infinite loop that will continue running until it’s explicitly broken. This is a common pattern when the number of iterations isn’t predetermined.
  2. User Input: user_input = int(input()) prompts the user to enter a number. The input() function reads the input as a string, and int() converts it to an integer. If the user enters a non-integer value, this will raise a ValueError. For a more robust program, you might want to include error handling to manage such cases, but for simplicity, it’s omitted here.
  3. Condition Check: if user_input < 0: checks if the entered number is negative. If it is, the break statement terminates the loop, and the program proceeds to the code following the loop.
  4. Loop Body Execution: If the entered number is non-negative, print("Body") executes, simulating the loop’s main action. In your specific case, it prints the string “Body”.
  5. Loop Termination Message: After exiting the loop (when a negative number is entered), print("Done.") executes, indicating the program has finished processing.

Sample Interaction:

9
Body
5
Body
2
Body
-1
Done.

In this interaction:

  • The user enters 9, 5, and 2, each of which is non-negative, so the program prints “Body” after each entry.
  • Upon entering -1, which is negative, the loop terminates, and the program prints “Done.”

Key Considerations:

  • Input Validation: The current implementation assumes the user will enter valid integers. To handle invalid inputs gracefully, you can incorporate a try-except block to catch ValueError exceptions when the input cannot be converted to an integer.
  • Loop Control: Using while True: with a break statement is a common idiom for loops that wait for a specific condition to exit, especially when the number of iterations isn’t known in advance.
  • Code Readability: Ensure your code is well-commented and follows consistent indentation practices, as Python relies on indentation to define code blocks.

By following this structure, you can create a loop that processes user input until a negative number is entered, at which point it terminates gracefully.

Scroll to Top