Neemaximo
Neemaximo

Reputation: 20831

Python while loop/range

I need to ask a number from the user and have then if it is in the range of the low/high number then it returns the number and if it isnt in the range, it loops until the number entered is in the range. I don't really know how exactly to do this but I think I have part of it right. My main concern is the line "while question != low <= question <= high:" I feel as if there is a problem with that line.

def ask_number(question, low, high):
    question = int(raw_input("Enter a number within the range: "))
    question = ""
    while question != low <= question <= high:
        question = int(raw_input("Enter a number within the range: "))

Upvotes: 0

Views: 12483

Answers (4)

Jack Edmonds
Jack Edmonds

Reputation: 33171

Your while loop syntax would be more clear if you thought of it this way: "I want to keep asking the user for the answer while their answer is less than low or greater than high." Translated directly to Python, this would be

while question < low or question > high:

You should also not assign "" to question as this overwrites the user's first answer. If they get the number in the range the first time, they will still be asked again. Basically, you should remove this line:

question = ""

Your final code should look something like this:

def ask_number(low, high):
    assert low < high
    question = int(raw_input("Enter a number within the range: "))
    while question < low or question > high:
        question = int(raw_input("Enter a number within the range: "))
    return question

print(ask_number(5,20))

Upvotes: 3

Tyler Crompton
Tyler Crompton

Reputation: 12662

def ask_number(low, high):

    """question cannot be less than the minimum value so we set it below the
    minimum value so that the loop will execute at least once."""

    question = low - 1

    """You want question to be within the range [high, low] (note the
    inclusivity), which would mathematically look like 'low <= question <= high'.
    Break that up into what appears to be one comparison at a time:
    'low <= question and question <= high'. We want the while loop to loop when
    this is false. While loops loop if the given condition is true. So we need to
    negate that expression. Using the DeMorgan's Theorem, we now have
    'low < question or question > high'."""

    while question < low or question > high:

        """And this will obviously update the value for question. I spruced up
        the only argument to raw_input() to make things a bit 'smoother'."""

        question = int(raw_input("Enter a number within the range [%d, %d]: " % _
                   (low, high)))

    # Return question.
    return question

Upvotes: 0

cetver
cetver

Reputation: 11829

def ask_number(low, high):  
    while True:
        number = int(raw_input('Enter a number within the range: '))
        if number in xrange(low, high + 1):
            return number

Upvotes: 0

Sven Marnach
Sven Marnach

Reputation: 601739

In this case, the easiest solution is to use True as the condition in the while loop, and an if inside the loop to break out if the number is fine:

def ask_number(low, high):
    while True:
        try:
            number = int(raw_input("Enter a number within the range: "))
        except ValueError:
            continue
        if low <= number <= high:
            return number

I also added a try/except statement to prevent the program from crashing if the user enters a string that can't be converted to a number.

Upvotes: 3

Related Questions