Python Forum
while with try and except gets stuck in an endless loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while with try and except gets stuck in an endless loop?
#1
I am working on input validation to validate integer input and index range. My def inputNumber(message) function works as I use it in other areas of my program and it functions as expected. However, my def withinRange(myList, choice) function throws an error appropriately but I get stuck in an endless loop. My code is provided below any help or suggestions are appreciated in advance.

def inputNumber(message):
    #input validation to ensure user enters a number
    while True:
        try:
            userInput = int(input(message))
        except ValueError:
            print('That is not a number! Try Again!')
            continue
        else:
            return userInput
            break
def withinRange(myList, choice):
    while True:
        try:
            result = myList[choice-1]
        except IndexError:
            print('That number is not a valid choice! Try Again!')
            continue
        else:
            return result
            break
choice = withinRange(resultsList,inputNumber('Select your new username by entering the number above:\n'))
Reply
#2
The first, inputNumber has a way of changing userInput
The second withinRange takes in a value that is not altered within the loop.
Reply
#3
Ask for a number within the while loop, not outside of it.

Like this:
def withinRange(myList, choice_getter, message):
    while True:
        choice = choice_getter(message)
        try:
            result = myList[choice-1]
        except IndexError:
            print('That number is not a valid choice! Try Again!')
            continue
        else:
            return result

choice = withinRange(resultsList, inputNumber, 'Select your new username by entering the number above:\n')
Or, if you're comfortable with partial functions, this is a cleaner looking way:
import functools

def withinRange(myList, choice_getter):
    while True:
        choice = choice_getter()
        try:
            result = myList[choice-1]
        except IndexError:
            print('That number is not a valid choice! Try Again!')
            continue
        else:
            return result

getter = functools.partial(inputNumber, 'Select your new username by entering the number above:\n')
choice = withinRange(resultsList, getter)
That's the same as this:
getter = lambda: inputNumber('Select your new username by entering the number above:\n')
Reply
#4
Nilamo the second option you provided worked like a charm. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 990 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Where's the endless loop? Mark17 14 3,690 Oct-04-2021, 06:54 PM
Last Post: Mark17
  Endless printing of a table djwilson0495 2 1,770 Aug-10-2020, 01:42 PM
Last Post: djwilson0495
  my function is stuck on loop - even when it not supposed to be korenron 2 2,324 May-26-2019, 12:31 PM
Last Post: korenron
  while loop issue - stuck in the loop! EricMichel 6 8,548 Aug-06-2018, 03:59 PM
Last Post: EricMichel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020