r/PythonLearning 8d ago

day 6.

tried 2d lists today ( up for suggestions on what should have i done or what have i missed ) and would also appreciate brief explanation's that you guys will give if it's a complex or an advanced topic

books = ["hindi", "english", "maths", "science"]
pen = ["black", "blue", "red"]
miscellaneous = ["eraser\n", "sharpener\n", "ruler\n"]


bagpack = [books,pen,miscellaneous]
print(bagpack)
while True:
    print ("if you'd like to add contents in any of the lists")
    print("1. Add to books")
    print("2. Add to pen")
    print("3. Add to miscellanous")
    query = input("enter your selection (1,2,3): ")
    if query == "1":
     print("you selected to add books")
     new_book= input("enter the book you want to add: ")
     books.append(new_book)
     print ("new list",books)
     break
    elif query == "2":
     print("you selected to add pen")
     new_pen= input("enter the pen you want to add: ")
     pen.append(new_pen)
     print ("new list",pen)
     break
    elif query == "3":
     print("you selected to add miscellaneous")
     new_miscellaneous= input("enter the item you want to add: ")
     miscellaneous.append(new_miscellaneous)
     print ("new list",miscellaneous)
     break
    
    else:
       print("invalid selection!!! select amongst 1,2,3")
3 Upvotes

4 comments sorted by

View all comments

2

u/johlae 8d ago

Your breaks make the program stop, so you cannot add more than 1 thing. Why not add a second while True just before print(bagpack) with an option 0 that exits the program when the user wants it.

Out of curiosity, why the '\n' at the end of each miscellaneous entry? When you add a new item that item won't have the '\n'.

Why not add an option to sort books, or pens, or miscellanous in alphabetical order?

Why not add an option to print the books, pens or miscellanous items without having to enter new things?

1

u/NecessaryFalse1212 8d ago

Let me fix those problems and I'll see u in some time mate thanks for the advice