0

This is the function that repeats (call get_next_value to get potential values!) until a valid value (a number in the range 1-26) is produced.Get_next_value is just a function. But it creates an infinite loop, how would i fix it?

while get_next_value(deck) < 27:
     if get_next_value(deck) < 27:
        result = get_next_value(deck)
return result

1 Answer 1

11

This is how it should be written:

while True:                        # Loop continuously
    result = get_next_value(deck)  # Get the function's return value
    if result < 27:                # If it is less than 27...
        return result              # ...return the value and exit the function

Not only is the infinite recursion stopped, but this method only runs get_next_value(deck) once per iteration, not three times.

Note that you could also do:

result = get_next_value(deck)      # Get the function's return value
while result >= 27:                # While it is not less than 27...
    result = get_next_value(deck)  # ...get a new one.
return result                      # Return the value and exit the function

These two solutions basically do the same thing, so the choice between them is simply one on style.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.