213k views
1 vote
P3: Convert Validator to a Function

Start Assignment
Due Mar 7 by 11:59pm
Points 70
Submitting
a text entry box or a file upload
Using the code for the integer validator B
1. turn it into a function (absolutely required, no custom function, no grade)
Parameters:
starting value (has a default of 1)
ending value (has a default of 100)
Return Value:
the valid integer
2. As this is an interactive script, you do NEED to use print statements inside, even if we are over using them in this example
3. It should prompt (via input() or print() as appropriate) the following while running
Ask them to enter an integer between "start value" and "end value"
Warn them if it is not a valid integer (already done)
Tell them if the integer they choose is NOT between "start value" and "end value"
To test the function:
1. Test the function by itself BEFORE you rewrite your guess a number program
2. Rewrite your number guess program to use the new function
3. your program should not be doing any of the prompts that are in the new function
So it should only be telling them if they are too high/low or if they got it
And how many guesses are left

1 Answer

1 vote

Final answer:

To convert the integer validator code into a function, follow these steps: create a new function, add parameters, use print statements to prompt the user, validate the input, check if the integer is between start and end values, and return the valid integer.

Step-by-step explanation:

In order to convert the integer validator code into a function, you will need to follow these steps:

  1. Create a new function called 'integer_validator'.
  2. Add two parameters to the function: 'start_value' and 'end_value', both with default values of 1 and 100 respectively.
  3. Within the function, use print statements to prompt the user to enter an integer between the start and end values.
  4. Validate if the input is a valid integer and warn the user if it is not.
  5. Check if the integer chosen by the user is between the start and end values and inform the user accordingly.
  6. Finally, return the valid integer as the return value of the function.

To test the function, you can call it with different parameter values and check if it behaves as expected.

User Dianne
by
7.7k points