Python Forum
build a list (add_animals) using a while loop, stop adding when an empty string is en
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
build a list (add_animals) using a while loop, stop adding when an empty string is en
#1
my question here
build a list (add_animals) using a while loop, stop adding when an empty string is entered
add_animals =[]
user_input=input("Enter animals names:")
while user_input!=" ":
    add_animals.append(user_input)
print(add_animals)
but I am not getting the proper output.
Reply
#2
You should get the user input in the loop.
Also, empty string is "", not " ". The latest being space, not empty string

add_animals =[]
while True:
    user_input=input("Enter animals names:")
    if user_input:
        add_animals.append(user_input)
    else:
        break
print(add_animals)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,720 Dec-11-2023, 08:27 AM
Last Post: buran
  Button to stop while loop from another script Absolutewind 5 877 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,629 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Help adding a loop inside a loop Extra 31 4,472 Oct-23-2022, 12:16 AM
Last Post: Extra
  -i option changes sys.path (removes leading empty string '') markanth 6 1,970 Aug-26-2022, 09:27 PM
Last Post: markanth
Question Keyword to build list from list of objects? pfdjhfuys 3 1,555 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,245 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  set.difference of two list gives empty result wardancer84 4 1,473 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  get out of while loop and stop repeat Frankduc 11 2,946 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Loop through a list of string oldtrafford 4 1,463 Mar-24-2022, 05:30 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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