0
# Feature to ask the user to type numbers and store them in lists
def asking_numbers_from_users():

    active = True

    while active:
        user_list = []
        message = input("\nPlease put number in the list: ")

        try:
            num = int(message)
            user_list.append(message)

        except ValueError:
            print("Only number is accepted")
            continue

        # Asking the user if they wish to add more

        message_1 = input("Do you want to add more? Y/N: ")

        if message_1 == "Y" or "y":
            continue

        elif message_1 == "N" or "n":

            # Length of list must be more or equal to 3
            
            if len(user_list) < 3:
                print("Insufficint numbers")
                continue

            # Escaping WHILE loop when the length of the list is more than 3

            else:
                active = False
        else:
            print("Unrecognised character")

    print("Merging all the numbers into a list........./n")
    print(user_list)

def swap_two_elements(user_list, loc1, loc2):

    loc1 = input("Select the first element you want to move: ")
    loc1 -= 1

    loc2 = input("Select the location you want to fit in: ")
    loc2 -= 1

    
    loc1, loc2 = loc2, loc1
    return user_list

# Releasing the features of the program
asking_numbers_from_users()
swap_two_elements
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • If you mean that it keeps looping even when pressing N, see [here](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-single-value). I can close it if that answers your question. – Carcigenicate Dec 04 '21 at 18:41
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Dec 10 '21 at 00:00

1 Answers1

0

I would break this up into more manageable chunks. Each type of user input can be placed in its own method where retries on invalid text can be assessed.

Let's start with the yes-or-no input.

def ask_yes_no(prompt):
    while True:
        message = input(prompt)
        if message in ("Y", "y"):
            return True
        if message in ("N", "n"):
            return False
        print("Invalid input, try again")

Note: In your original code you had if message_1 == "Y" or "y". This does not do what you think it does. See here.

Now lets do one for getting a number:

def ask_number(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print("Invalid input, try again")

Now we can use these method to create you logic in a much more simplified way

def asking_numbers_from_users():
    user_list = []
    while True:
        number = ask_number("\nPlease put number in the list: ")
        user_list.append(number)

        if len(user_list) >= 3:
            if not ask_yes_no("Do you want to add more? Y/N: ")
                break

    print("Merging all the numbers into a list........./n")
    print(user_list)
flakes
  • 21,558
  • 8
  • 41
  • 88