0

Issue is when I test my error handling, entering the unexpected inputs multiple times and then proceeding to enter the acceptable number of lines to bet on the return is recived as "None" the variable seems to not stay stored or is somehow over written. Any Ideas? I would place the other half of the code but am forced to type everything due to company policies. I apologize for any inconvenience, I thank everyone for ther time in viewing this post.

MAX_LINES = 3

def get_number_of_lines():
    try:
        while True:
            lines = ("Enter the number of lines to be on (1-" + str(MAX_LINES) + ")? ")
            if lines.isdigit:
                lines = int(lines)
                if 1 <= lines <= MAX_LINES:
                    break
            else:
                print("Enter a valid number of lines.")
        return lines
    except ValueError:
        print("Please enter a number. ")
        get_number_of_lines()


def main():
    lines = get_number_of_lines()
    print(lines)

Expected the number entered 1-3 to be printed from the return line even if moved from and to the except statment.

  • 2
    Two things: 1. The recursive call needs a `return` before it. 2. Don't use recursion, use a loop. – Barmar Dec 28 '22 at 22:18

1 Answers1

0

Add return before get_number_of_lines(). Otherwise your function will return None.

print("Please enter a number")
return get_number_of_lines()
Alderven
  • 7,569
  • 5
  • 26
  • 38