r/PythonLearning 2d ago

New update with (def), soon!

9 Upvotes

8 comments sorted by

u/Sea-Ad7805 2d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20with%20open(tasks_json%2C%20%22r%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20tasks_list%20%3D%20json.load(file)%0Aelse%3A%0A%20%20%20%20with%20open(tasks_json%2C%20%22w%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20json.dump(tasks_list%2C%20file)%0A%0A%0Awhile%20True%3A%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20need%20%3D%20int(input(%0A%20%20%20%20%20%20%20%20%20%20%20%20%221-%20ADD%20TASK!%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%222-%20Show%20my%20shits!%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%223-%20kill%20one!%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%224-%20I%20know%20my%20shit%2C%20getout*%20n%22%0A%20%20%20%20%20%20%20%20))%0A%0A%20%20%20%20%20%20%20%20if%20need%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20i_task%20%3D%20input(%22type%20your%20task%3A%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20tasks_list.append(i_task)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20open(tasks_json%2C%20%22w%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20json.dump(tasks_list%2C%20file)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22task%20%7Bi_task%7D%20added!%22)%0A%0A%20%20%20%20%20%20%20%20elif%20need%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20open(tasks_json%2C%20%22r%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tasks_list%20%3D%20json.load(file)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20len(tasks_list)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22still%20there%20is%20no%20tasks!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20index%2C%20task%20in%20enumerate(tasks_list)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22my%20task%20is%3A%20%7Bindex%7D-%20%7Btask%7D%22)%0A%0A%20%20%20%20%20%20%20%20elif%20need%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dele%20%3D%20int(input(%22which%20one%3F%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20tasks_list.pop(dele)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20open(tasks_json%2C%20%22w%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20json.dump(tasks_list%2C%20file)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bdele%7D%20had%20been%20killed!%22)%0A%0A%20%20%20%20%20%20%20%20elif%20need%20%3D%3D%204%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22don't%20come%20here%20before%20getting%20done!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22you%20see%20what%20i%20don't%20see%3F%22)%0A%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22just%20pick%20a%20number!%22)&timestep=1&play) to see the program state change step by step.

→ More replies (1)

2

u/FoolsSeldom 2d ago

Any reason for not sharing the code instead of an image? Perhaps telling us what it is about and why you are sharing?

1

u/the_meteor_beat 2d ago

Still didn't understand the first question, about the second, this sub called learning python, when i post the first post i got some help, so i will post another one after hours also for the same reason.

1

u/FoolsSeldom 1d ago

OK. Good you've updated your post to include your code, or at least images of your code. Any chance you could actually share the code itself - much easier to review and help when you share actual code.

The only code you've shared is partial:

# Lines 1–22 not visible in the screenshot.
# Line 23 (partially obscured):
need = int(input(" 1- ADD TASK! \n 2- Show my shits! \n 3- kill one! \n 4- I know my shit, getout* \n"))

if need == 1:
    i_task = input("type your task: ")
    tasks_list.append(i_task)
    with open(tasks_json, "w") as file:
        json.dump(tasks_list, file)
    print(f"task {i_task} added!")

elif need == 2:
    with open(tasks_json, "r") as file:
        tasks_list = json.load(file)
    if len(tasks_list) == 0:
        print("still there is no tasks!")

    else:
        for index, task in enumerate(tasks_list):
            print(f"my task is: {index}- {task}")


elif need == 3:

    dele = int(input("which one?"))
    tasks_list.pop(dele)
    with open(tasks_json, "w") as file:
        json.dump(tasks_list, file)
    print(f"{dele} had been killed!")


elif need == 4:
    print("don't come here before getting done!")
    break


else:
    print("you see what i don't see?")


except ValueError:
    print("just pick a number!")

and I am wondering why you are converting input to int when you aren't doing maths with it. What's wrong with checking for == "1" and avoiding possible bad inputs.

2

u/the_meteor_beat 1d ago

I understand now, about converting, still learning bro, i will try your way. And i see now sharing the code will be useful than just an image about it.

1

u/voices_38 2d ago

who’s JSON?

1

u/the_meteor_beat 2d ago

To save what user add in the hard disk Instead of ram.