Search code examples
arraysfor-looppython-3.7dicecoding-efficiency

How to compare user input to an array to increase efficency


I'm trying to increase the efficiency of some working code or reduce the number of loops required. Is there a way to use an array or list to validate user input?

So far I've managed to get the code working with a for-else-loop, but it looks cumbersome and not very pythonic. I can't find any answers that involve comparing an array to user input. The code should check if user input matches the right number of sides for valid dice. (So they can't put something like 3 sides)

def check(die):
    if die == 100 or die == 20 or die == 12 or die == 10 or die == 8 or die == 6 or die == 4:
        legal = True
    else:
        legal = False
    return legal

Solution

  • Try this one:

    def check(die,List_Of_Values):
        if die in List_Of_Values:
            legal = True
        else:
            legal = False
        return legal
    List_Of_Values=[100,20,12,10,8,6,4]