When I run this and I enter a value for start that is greater than end it asks me to re enter them but it asks me to enter end twice should be once

Viewed 21

I truly have no clue why it goes back the the 2nd while loop again after I have returned start and end.

def num():

    while True:
        start = input("Enter the start of the range: ")
        if start.isdigit():
            while True:
                end = input("Enter the end of the range: ") 
                if end.isdigit():
                    if start <= end:
                        return start, end;
                    else:
                        print("Please enter a valid number.")
                        num()
                else:
                    print("Please enter a valid number.")
        else:
            print("Please enter a valid number.")

num_lst = list(num())
            
1 Answers

In case of start > end you call num() again which brings you back to the first while loop.

You're almost there! You actually just don't need to call num() and it'll work.

def num():
    while True:
        start = input("Enter the start of the range: ")
        if start.isdigit():
            while True:
                end = input("Enter the end of the range: ")
                if end.isdigit():
                    if start <= end:
                        return start, end
                    else:
                        print("Please enter a valid number.")
                else:
                    print("Please enter a valid number.")
        else:
            print("Please enter a valid number.")


num_lst = list(num())
Related