strings and ints consistency - isinstance

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Sep 22 08:58:34 EDT 2016


> On Wednesday, September 21, 2016 at 11:41:42 PM UTC-4, Sayth Renshaw wrote:
> 
>>answer = input("\t >> ")
>>if isinstance(int(answer), int) is True:
>>    raise ValueError("Ints aren't valid input")

You seem to be trying to check that the user hasn't entered
an integer. But that's a backwards way of looking at it. If
the only valid inputs at that point are "y" or "n", then
you should check for those specifically:

    answer = input("\t >> ")
    if answer == "y":
       # do the "yes" thing
    elif answer == "n":
       # do the "no" thing
    else:
       # user entered something wrong

-- 
Greg



More information about the Python-list mailing list