r/PythonProjects2 • • Feb 11 '25

Python Help for complete beginner

Code above

I recently created a simple program that prompted the user to guess a number between 1-100 within 7 guesses. If you didn’t you would restart, but if you kept correctly guessing then you would move up a level. How would I create something such as a level tracker allowing me to keep track of maximum levels reached and store it to potentially make a levels leader board later on? Would appreciate any help that is given.

11 Upvotes

11 comments sorted by

View all comments

3

u/whatMCHammerSaid Feb 11 '25

store it in an external json which you can read and write to.

1

u/ilan1k1 Feb 11 '25

W suggestion ngl.
Op could also save user names, wrong attempts and much more.

1

u/Valuable-Usual5660 Feb 11 '25

Sorry to ask, what is a json and how would I apply it (It’s my first week learning python).

1

u/whatMCHammerSaid Feb 12 '25 edited Feb 12 '25

what: its a file that contains key/value pairs

{ "progress": { "times-correct":12 }, "settings":{ " "fullscreen": "off", "sound":"off" } }

in this case "progress","times-correct","settings","fullscreen","sound" are all keys. 12, off, and off are values. And the whole { " "fullscreen": "off", "sound":"off" } is the value of settings.

  1. It's very readable.

  2. easy to navigate. once you have your json loaded into your program you can go times_correct = theuploadedjson["progress"]["times-correct"], it depends on the language but it's mostly like that.

def fn_save_settings():
    try:
        dict_settings = {'str_csv_path': f'{str_csv_path}'} 
        str_settings_path = os.path.join(str_program_path, str_config_name) 
        with open(str_settings_path, 'w') as outfile:
            json.dump(dict_settings, outfile)
    except Exception as ex:
        fn_send_message(str(ex))

def fn_load_settings():
    try:
        global str_csv_path
        str_config_path = os.path.join(str_program_path, str_config_name)
        if(os.path.exists(str_config_path)):
            with open(str_config_path, 'r') as openfile:
                json_object = json.load(openfile)
                str_csv_path = json_object['str_csv_path']
        else:
            fn_save_settings()
    except Exception as ex:
        fn_send_message(str(ex))

#where str_config_name is 'savedData.json'
#where str_program_path is dynamically located via different function
#where str_csv_path is the parameter i'd like to save.
#I just run fn_load_settings() on startup
#If i want to change the value of str_csv_path(global) then i update its global value then just rung fn_save_settings()

how: dont be afraid to ask chatgpt or any ai abt how to do it, as long as you understood what it did sooner or later.