Home > Software engineering >  While Loop Not Working As Intended in Python
While Loop Not Working As Intended in Python

Time:11-13

I need my program to show a list and then ask the user if they want to add anything; then it adds that input to the list. It then asks the user again in separate input if they want to add anything else and if they hit enter it prints the list including all the new inputs and ends the big while loop.

list1 = [1,2,3,4,5,6]

def list_adder(liist):
    print("Here is the list:\n")
    def show_list():
        for element in liist:
            print(element)
    show_list()
    x = True
    while x == True:
        counter = 0
        if counter == 0:
            add_input1 = input("\nWhat would you like to add:\n")
            liist.append(add_input1)
            counter  1
        while counter == 1:
            add_input2 = input("\nWhat else would you like to add to the list?: \n")
            to_do_list.append(add_input2)
            if not add_input2:
                show_list()
                counter  = 1
                x == False
list_adder(list1)

I tried this but it keeps saying "What would you like to add" over and over

CodePudding user response:

Make counter = counter 1 and try rerunning the code.

Also, please change x == False to x = False

There would be other errors related to to_do_list not being defined. I believe you need to change it to liist.

The below code should work fine for your requirements:

list1 = [1,2,3,4,5,6]

def list_adder(liist):
    print("Here is the list:\n")
    def show_list():
        for element in liist:
            print(element)
    show_list()
    x = True
    while x == True:
        counter = 0
        if counter == 0:
            add_input1 = input("\nWhat would you like to add:\n")
            liist.append(add_input1)
            counter = counter  1
        while counter == 1:
            add_input2 = input("\nWhat else would you like to add to the list?: \n")
            liist.append(add_input2)
            if not add_input2:
                show_list()
                counter  = 1
                x = False

CodePudding user response:

The incremented counter variable needs to be assigned back to itself as below.

if counter == 0:
    add_input1 = input("\nWhat would you like to add:\n")
    liist.append(add_input1)
    counter  = 1

CodePudding user response:

Delete counter 1 under liist.append(add_input1), because I've changed the condition from while counter == 1 to while counter == 0. There is a small error at the line list1.append(add_input2), you wrote some invalid names so Python raises the error. Do not do x == False, you are comparing the values, not assigning them. Rather do x = False, where you assign x to false.

list1 = [1,2,3,4,5,6]

def list_adder(liist):
    print("Here is the list:\n")
    def show_list():
        for element in liist:
            print(element)
    show_list()
    x = True
    while x == True:
        counter = 0
        if counter == 0:
            add_input1 = input("\nWhat would you like to add:\n")
            liist.append(add_input1)
        while counter == 0:
            add_input2 = input("\nWhat else would you like to add to the list?: \n")
            list1.append(add_input2)
            if not add_input2:
                print(show_list())
                counter  = 1
                x = False
list_adder(list1)

CodePudding user response:

Issues In Current Code:

There are several issues with your current approach. These are as follows:

  • counter 1 calculates a new value but does not change the value of the counter variable. E.g. if counter is 0 then counter 1 will give you the value 1 but the variable counter will still be 0. To correct this you should use counter = 1.

  • It seems your list will be storing integer values only. If this is the case, the value read in from the user should be converted to an integer value using the int() function.

  • The variable to_do_list is not defined before its use and would result in an error. I assume you meant liist.

  • while x == True can be replaced with while x.

  • x == False should be x = False if you wish to set the variable x to the value False.

  • The code contains some unnecessary nesting. You could, for example, remove the innermost while loop and instead have a single loop with appropriate logic to achieve the same result.

  • An alternative approach to using a counter variable is to simply check if the list size has been changed.

Improved Solution:

The solution below addresses the issues above and demonstrates how the solution can be simplified:

def append_list(the_list):
  initial_size = len(the_list)
  print(f"The list currently contains {initial_size} values as follows: \n{the_list}")
  
  while True:
    variation = "would" if len(the_list) == initial_size else "else would"
    value = input(f"What {variation} you like to add?")
    
    if value:
      the_list.append(int(value))
    else:
      print(f"The final list contains {len(the_list)} values as follows: \n{the_list}")
      return

user_list = [1, 2, 3, 4, 5, 6]
append_list(user_list)

Additional Information:

If you would like to print each value in the list on a new line then you can do the following:

print(*the_list, sep="\n")

The above statement will unpack the list and then print each item using the new line separator.

  • Related