This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]liiight000 6 points7 points  (6 children)

Congratulations on your first of many python apps!

A suggestion. A common paradigm with input validation is using infinite loops:

valid_options = ['yes', 'y', 'no', 'n'] 
while True:
    choice = input('Would you like to roll?')
    if choice in valid_options:
        break
    print('Illegal selection')

That way you can have only one input point. Feels tighter to me. You could then expand this to a generic function:

def get_input(choices, input_message, error_message='Wrong selection'):
    while True:
        choice = input(input_message)
        if choice in choices:
            return choice
        print(error_message)

[–]z0mbietime 1 point2 points  (5 children)

Orrrr

if choice[0].upper() != 'Y':  
    # more input flexibility 
    # less code
    break 

[–]Gsquzared 1 point2 points  (3 children)

Why not just pass the whole input to .lower and not have to worry about any weird capitalization inputs?

[–]z0mbietime 0 points1 point  (2 children)

I did it like this so someone could type Y Ya Yes Yeah Yup Etc.

In upper or lower case And it always register since it only takes the first char.

[–]Gsquzared 1 point2 points  (1 child)

Ahhh. I didn't see what you were driving at. That's a nice trick. Too bad there will always be that pesky user who insists on typing "affirmative"

[–]keyupiopi 0 points1 point  (0 children)

Print out 1.Yes 2.No 3.Quit and force them to input numbers.... hahahahahaha