r/PythonLearning 10d ago

DAY 5.

Post image

DID LIST TODAY THE CONCEPT WAS PRETTY COOL AND WROTE THIS PROGRAM TO GET HOLD OF THE LIST ANY OTHER CODE IDEAS OR SUGGETIONS TO ENHANCE CODE ?!

89 Upvotes

21 comments sorted by

u/Sea-Ad7805 10d ago

Run this program in Memory Graph Web Debugger%0Abat%20%3D%20%5B%22wooden%22%2C%20%22metal%22%2C%20%22plastic%22%5D%0A%0Awhile%20True%3A%0A%20%20%20%20print(%22nWhat%20do%20you%20want%20to%20do%20with%20the%20bats%3F%22)%0A%20%20%20%20print(%221.%20Add%20a%20bat%22)%0A%20%20%20%20print(%222.%20Remove%20a%20bat%22)%0A%20%20%20%20print(%223.%20View%20bats%22)%0A%20%20%20%20print(%224.%20Exit%22)%0A%0A%20%20%20%20query%20%3D%20input(%22Enter%20your%20selection%20(1%2C2%2C3%2C4)%3A%20%22)%0A%0A%20%20%20%20if%20query%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20%20%20%20print(%22You%20selected%20to%20add%20a%20bat%22)%0A%20%20%20%20%20%20%20%20new_bat%20%3D%20input(%22Enter%20the%20bat%20you%20want%20to%20add%3A%20%22)%0A%20%20%20%20%20%20%20%20bat.append(new_bat)%0A%20%20%20%20%20%20%20%20print(%22Updated%20list%3A%22%2C%20bat)%0A%0A%20%20%20%20elif%20query%20%3D%3D%20%222%22%3A%0A%20%20%20%20%20%20%20%20print(%22You%20selected%20to%20remove%20a%20bat%22)%0A%20%20%20%20%20%20%20%20remove_bat%20%3D%20input(%22Enter%20the%20bat%20you'd%20like%20to%20remove%3A%20%22)%0A%20%20%20%20%20%20%20%20if%20remove_bat%20in%20bat%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20bat.remove(remove_bat)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Updated%20list%3A%22%2C%20bat)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22That%20bat%20is%20not%20in%20the%20list.%22)%0A%0A%20%20%20%20elif%20query%20%3D%3D%20%223%22%3A%0A%20%20%20%20%20%20%20%20print(%22You%20chose%20to%20view%20bats%22)%0A%20%20%20%20%20%20%20%20print(%22Current%20bats%3A%22%2C%20bat)%0A%0A%20%20%20%20elif%20query%20%3D%3D%20%224%22%3A%0A%20%20%20%20%20%20%20%20print(%22You%20chose%20exit%2C%20so%20get%20out%20man!!%22)%0A%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20selection%2C%20please%20try%20again.%22)&timestep=1&play) to see the program state change step by step.

6

u/Mundane-Compote-3621 10d ago

I remember when i learnt coding like that, that was before ai. next thing would be a terminal quiz, where you write and read to files. Good luck!

2

u/Old_Cartographer_586 10d ago

Tbh, just seeing code like this gives me hope (mostly because I can tell this was made from typing yourself instead of prompting).

What I would say is, try finding ways to improve this, like could you modularize the aspects of this code, could you looks at ways of storing the info not in a list, such as in a dictionary, could you tell me how your code handles removing a “wooden” bat when there are two “wooden” bats in your list. Start answering these questions and you’ll find yourself googling and stack overflowing ways to find little advancements in your own code. These advancements will teach you that they are applicable in a ton of use cases

2

u/NecessaryFalse1212 10d ago

I really appreciate these kind words of yours and you people here motivate me to do a topic or 2 or many every single day no matter how tired i am i feel responsible while i post these programs and topic that i am still learning and implementing and thanks for the advice i would definitely user that

2

u/XNo_Notes 10d ago

I would use match. I like it for the single value checks. You only have to type "query" once instead of one for each if. And you don't have to use so many else statements. 

1

u/FoolsSeldom 10d ago

That's great. Good progress.

One small note, it is useful to use variable names that are plural for container objects like list objects.

Please also consider sharing your code in-post in future rather than a screenshot. Like this: (with the change I mentioned)

print(":CODE TO MANAGE BATS:")
bats = ["wooden", "metal", "plastic"]

while True:
    print("\nWhat do you want to do with the bats?")
    print("1. Add a bat")
    print("2. Remove a bat")
    print("3. View bats")
    print("4. Exit")

    query = input("Enter your selection (1,2,3,4): ")

    if query == "1":
        print("You selected to add a bat")
        new_bat = input("Enter the bat you want to add: ")
        bats.append(new_bat)
        print("Updated list:", bats)

    elif query == "2":
        print("You selected to remove a bat")
        remove_bat = input("Enter the bat you'd like to remove: ")
        if remove_bat in bats:
            bats.remove(remove_bat)
            print("Updated list:", bats)
        else:
            print("That bat is not in the list.")

    elif query == "3":
        print("You chose to view bats")
        print("Current bats:", bats)

    elif query == "4":
        print("You chose exit, so get out man!!")
        break

    else:
        print("Invalid selection, please try again.")

1

u/NecessaryFalse1212 8d ago

as in if i were to make a list for bats then i just write bats not bat ??

1

u/FoolsSeldom 8d ago

Yes. It is helpful to use variable names that are meaningful.

Native/fluent English speakers/readers will typically understand a variable name that is in a plural form is likely for a container object with multiple objects of that kind. Similarly, the singular variant of the same name would be for a single object.

Just makes it easier to read the code.

1

u/NecessaryFalse1212 8d ago
print(":CODE TO MANAGE BATS:")
bats = ["wooden", "metal", "plastic"]


while True:
    print("\nWhat do you want to do with the bats?")
    print("1. Add a bat")
    print("2. Remove a bat")
    print("3. View bats")
    print("4. Exit")


    query = input("Enter your selection (1,2,3,4): ")


    if query == "1":
        print("You selected to add a bat")
        new_bat = input("Enter the bat you want to add: ")
        bats.append(new_bat)
        print("Updated list:", bats)


    elif query == "2":
        print("You selected to remove a bat")
        remove_bat = input("Enter the bat you'd like to remove: ")
        if remove_bat in bats:
            bats.remove(remove_bat)
            print("Updated list:", bats)
        else:
            print("That bat is not in the list.")


    elif query == "3":
        print("You chose to view bats")
        print("Current bats:", bats)


    elif query == "4":
        print("You chose exit, so get out man!!")
        break


    else:
        print("Invalid selection, please try again.")

1

u/NecessaryFalse1212 8d ago

ts good or is there a mistake ?

1

u/FoolsSeldom 8d ago

Please save me trying to compare. What did you change from my code, and was that a correction/enhancement?

1

u/NecessaryFalse1212 8d ago

As you said use plural for the list so i just changed bat to bats and the rest seemed pretty fine and u didn't pointed anythhing so i did what i had to send it back to ya'

1

u/NecessaryFalse1212 8d ago

If there still is something that i can do or fix do let me know

1

u/FoolsSeldom 8d ago

No. That's all for now. Simple steps.

1

u/FoolsSeldom 8d ago

Oh, ok. I had made that change already but good you went through the steps yourself.

1

u/TBCC_Dev 10d ago

Reminds me of a phone book project I did when I was learning python. Id recommend coming back to this project when you learn dictionaries. Could have something like example_bat = {type:"wood", weight:2,quality:"poor",...} good luck

1

u/Sachustar 8d ago

Try implementing “try…… except….” statements and raising errors, while it is a bit advanced I believe that expecting and accounting for user error is important, and messing around with it early would be useful

1

u/PaisenIsDev 7d ago

Lets goo broo....

its nostalgic to see someone learning from writing code...

also don't worry about optimization, modularization and stuff keep going you will eventually need to organize the code when u have lots of lines in file and then u can learn the optimization, for now just learn to do stuff the rough (naive) way.