How to Create a List of Items in Python Based on User Input?

Estimated read time 1 min read

You can create a list of items in Python based on user input by using the input() function to prompt the user to enter values, and then appending those values to a list using the append() method. Here’s an example code:

items = []  # create an empty list

# prompt the user to enter items until they type "done"
while True:
    user_input = input("Enter an item or type 'done' to exit: ")
    if user_input == "done":
        break  # exit the loop if user types "done"
    items.append(user_input)  # add the item to the list

print("Your list of items is:")
print(items)  # print the final list of items

In this example, the while loop continues prompting the user to enter items until they type “done”. Each item entered by the user is added to the items list using the append() method. Finally, the code prints the complete list of items entered by the user using the print() function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply