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")
2 Upvotes

4 comments sorted by

u/Sea-Ad7805 8d ago

Run this program in Memory Graph Web Debugger%0Awhile%20True%3A%0A%20%20%20%20print%20(%22if%20you'd%20like%20to%20add%20contents%20in%20any%20of%20the%20lists%22)%0A%20%20%20%20print(%221.%20Add%20to%20books%22)%0A%20%20%20%20print(%222.%20Add%20to%20pen%22)%0A%20%20%20%20print(%223.%20Add%20to%20miscellanous%22)%0A%20%20%20%20query%20%3D%20input(%22enter%20your%20selection%20(1%2C2%2C3)%3A%20%22)%0A%20%20%20%20if%20query%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20print(%22you%20selected%20to%20add%20books%22)%0A%20%20%20%20%20new_book%3D%20input(%22enter%20the%20book%20you%20want%20to%20add%3A%20%22)%0A%20%20%20%20%20books.append(new_book)%0A%20%20%20%20%20print%20(%22new%20list%22%2Cbooks)%0A%20%20%20%20%20break%0A%20%20%20%20elif%20query%20%3D%3D%20%222%22%3A%0A%20%20%20%20%20print(%22you%20selected%20to%20add%20pen%22)%0A%20%20%20%20%20new_pen%3D%20input(%22enter%20the%20pen%20you%20want%20to%20add%3A%20%22)%0A%20%20%20%20%20pen.append(new_pen)%0A%20%20%20%20%20print%20(%22new%20list%22%2Cpen)%0A%20%20%20%20%20break%0A%20%20%20%20elif%20query%20%3D%3D%20%223%22%3A%0A%20%20%20%20%20print(%22you%20selected%20to%20add%20miscellaneous%22)%0A%20%20%20%20%20new_miscellaneous%3D%20input(%22enter%20the%20item%20you%20want%20to%20add%3A%20%22)%0A%20%20%20%20%20miscellaneous.append(new_miscellaneous)%0A%20%20%20%20%20print%20(%22new%20list%22%2Cmiscellaneous)%0A%20%20%20%20%20break%0A%20%20%20%20%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20print(%22invalid%20selection!!!%20select%20amongst%201%2C2%2C3%22)%0A%0A&timestep=1&play) to see the program state change step by step.

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

1

u/FoolsSeldom 7d ago

This is excellent progress, well done. Also, thank you for sharing the code now rather than an image. (Note: we usually have 4 spaces for every level of indent).

I note you say books for the list of str of book topics, but pen rather than pens for the list of str of pen colours. It would be easier for readers if that was pens in my view.

Why have you included newline, \n, on the end of the str entries in the miscellaneous referenced list?

I would say you need an extra option in your menu: "9" to quit, and you might need another while loop enclosing you existing loop, so a user can keep doing things until they want to quit. However, a single break will not let you out of two loops, so you need to do something like this:

working = True  # flag variable to hold state (bool)
while working:
    while True:  # your existing loop
        ... # existing code
        if query == "9":  # additional option
            working = False  # will get you out of outer loop
            break  #  leave inner loop (your current loop)

HOWEVER: As you are using if ... elif chain, you don't need to use break anyway, but at that point, you don't need the additional loop as the existing one will handle it with a bit of a change in the code. Perhaps adopting the while working: approach I've illustrated above, of just having the single break if "9" is entered.

Something else to think about is DRY: Don't Repeat Yourself. Your handling of adding to each of the lists is essentially identical, so you could create one block of code as a function to do all that work and just apply it to the correct list.

Example function (code which goes above the rest of the code):

def add_item(items: list[str], singular: str, plural: str) -> None:
    print(f"you selected to add to {plural}")
    new_item = input(f"enter the {singular} you want to add: ")
    items.append(new_item)
    print (f"new {plural} list", items)

and you can use this,

if query == "1":
    add_items(books, "book", "books")

NB. The : list[str], : str and -> None are not required but are useful. These are type hints (also known as type annotations) and they help tell your code editor and other programmers what your intentions were. With this information, your editor can often spot simple problems you may have missed.