[Tutor] Validating specific integer input [Was: Re: Converting a txt file to dictionary]

boB Stepp robertvstepp at gmail.com
Sat Aug 29 12:04:08 EDT 2020


It might have been better if you had created a new thread as your
question (statement) below is wholly unrelated to this subject line.

On Sat, Aug 29, 2020 at 2:38 AM nzbz xx <nzbzxx at gmail.com> wrote:
>
> I'm trying to create a def function code to validate user input. I would
> want the user to only input either 1 or 2. Any other inputs would reprompt
> the user again. This is what i have so far:
>
>  def data_validation(User_Decision):
>     try:
>         if User_Decision != 1 or User_Decision != 2:
>             print("Please enter option number: 1 or 2.")
>     except ValueError:
>         print("Input is invalid. Please try again.")

Think about this for a moment.  Inside your function above, what
statement would ever produce a ValueError exception?  If unsure
experiment with a statement in the interpreter.  Also, what if it *is*
valid user input?  How would this function *return* the validated user
input back to the code that called it?

> User_Decision = int(input(">"))

You use this in your while loop below, but without the enclosing
"int()".  Is it really needed in both places?  BTW, would the above
statement ever generate a ValueError exception?

> while data_validation(User_Decision):
>     User_Decision = input(">")

We have reached the end of the code you provided.  Have you run this
code?  What happened?  Is it what you expected?

Notice that you have never called your validation function, so it is
doing nothing for you.  Where would the logical place to call it and
validate user input?

Another question:  Will your while loop ever end?  Will the user ever
be able to successfully enter input and have your program continue to
process the user input?  How should you exit your loop upon successful
user input validation?  Should your loop be the place to call your
user validation function?  And where should "int()" be used?  In the
loop?  Or in your function?  Isn't calling "int()" on user input the
way a ValueError may arise?

A thought:  If I were the user, with the code you have showed us, I
would have no clue as to what you wanted me to enter for input.  Do
you have an introduction displayed (that you haven't shown us) that
explains this to the user?

Another question for your input validation:  Do you need to worry
about the user entering floats?  I suspect not as int() conveniently
truncates decimal places from entered floats, but you should at least
briefly think about it.

I apologize in advance that I have not provided any answers, only
questions, but I believe that if you consider the questions you will
see what you need to do to achieve your desired result.  If you get
stuck submit your new code and any exception tracebacks (fully
cut-and-paste into your email please) and I am sure we will get you
where you would like to be going.

HTH!


-- 
boB


More information about the Tutor mailing list