[Tutor] validating user input

Rance Hall ranceh at gmail.com
Sun Apr 24 00:31:41 CEST 2011


Ok, so I have this code that is working, but does little to validate
user input and I don't quite yet understand that process yet.

so given the following function:

def buildmenu(menuchoices):
    for i, option in enumerate(menuchoices, 1):
        print('%s. %s' % (i, option))
    menuchoice = int(input('\nYour Choice? '))
    return menuchoice-1

Ideally before returning the menuchoice-1 there should be a check to
make sure that the choice is a valid one.

I know what I want to happen if the test fails, and if it passes we
can just do the return and exit the function.

I need help understanding how exactly we validate the input since in
this case I have no idea how many menu entries there would be.

Thanks for your time.

Rance

PS  Happy Easter.


More information about the Tutor mailing list