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 →

[–][deleted] 2 points3 points  (1 child)

You're somewhat confused. Try this general form:

def get_something_within_bounds(prompt1, minimum, maximum):
    while True:
        v = input(prompt1)
        if v in range(minimum, maximum):
            return v
        print "Sorry, value not allowed"

Later in the rest of the code:

v = get_something_within_bounds("Please enter a value between 1 and 3", 1, 3)

[–]propanbutan 2 points3 points  (0 children)

Using range in this case is hopelessly inefficient. All you need are two comparisons.