Showing posts with label data entry. Show all posts
Showing posts with label data entry. Show all posts

Problem Solving With Python: Program 6

The problem statement:

We are tasked with prompting a user to enter a specific type of value, validating the value and print a message appropriate to the value provided. The following are a set of requirements for this task:

  1. Prompt the user to only enter a non-negative integer. For example, the following are examples of non-negative integers. 0, 1, 2, 3, 4, ...
  2. If the user successfully enters a non-negative integer:
    1. Display a success message
    2. Terminate the program.
  3. If the user fails to enter a non-negative integer then repeat the program from step one.

 The following is the expected approximate output of program 6 when an incorrect value type is provided:

Please enter a non-negative integer >> hello
Error: 'hello' is not a non-negative integer.
Please enter a non-negative integer >>


The following is the expected approximate output of program 6 when a correct value type is provided:

Please enter a non-negative integer >> 22
Success: '22' is a non-negative integer.
Goodbye!

The Solution:

def request_a_positive_integer_from_user():
    prompt_message = "Please enter a non-negative integer >> "
    while True:
        user_value = input(prompt_message)
        if user_value.isdigit():
            success_message = f"Success: '{user_value}' is a non-negative integer."
            print(success_message)
            print("Goodbye!")
            break
        else:
            error_message = f"Error: '{user_value}' is not a non-negative integer."
            print(error_message)


def request_a_positive_integer_from_user_alternate():
    prompt_message = "Please enter a non-negative integer >> "
    require_input_value = True
    while require_input_value:
        user_value = input(prompt_message)
        if user_value.isdigit():
            success_message = f"Success: '{user_value}' is a non-negative integer."
            print(success_message)
            print("Goodbye!")
            require_input_value = False
        else:
            error_message = f"Error: '{user_value}' is not a non-negative integer."
            print(error_message)


if __name__ == '__main__':
    request_a_positive_integer_from_user()
    # request_a_positive_integer_from_user_alternate()
In the previous example we saw how to use a while loop to compute powers of two less than 1,000. This solution will also use a while loop in order to satisfy the requirement (requirement 3) that the user be repeatedly prompted to enter a non-negative integer in the event that a different type of value is entered. We accomplish this by using a while loop with a static or fixed conditional statement of True. Recall that a while loop will not terminate until its conditional statement evaluates to False. Since we did not use a variable in the while loop's conditional statement we have no way to programmatically signal to the while loop to terminate. Instead we use a break statement to perform the while loop termination.

What is a break statement?
A break statement is used to signal to the Python interpreter to terminate the current loop; it can also be used with a for loop. We could have instead used an additional variable to signal to the loop to terminate (instead of signaling to the Python interpreter to terminate). The use of the break statement allowed us to use one less variable thus making the solution a little bit more concise. See the alternate solution, request_a_positive_integer_from_user_alternate(), that utilizes a variable instead of a break statement.

When to use a break statement vs a variable?
The choice is really yours... For a simple solution as this one the break statement suffices. But if the logic in the while loop were more involved and required significantly more lines then we would have opted for a more descriptive approach so that the code is easier to read and follow the logic flow.

How do we prompt the user for a value?

The Python language has a built in function called input(...). When this function is invoked (or called) the execution of our solution is paused at this line (or point) of invocation. The function displays to the user our custom message and waits for the user to either press the Enter key or input a value followed by the press of the Enter key. Control is given back to our solution once the user presses the Enter and our solution logic continues to execute. At this point we are required to validate the user input and take appropriate action.

How do we validate the user input?
Given a string, Python has a builtin function that can tell us if the entire string is a sequence of characters that can be converted to a non-negative integer; True is returned if this possible else False is returned by the isdigit() function.

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.