0
import json
with open("login_data.txt", "r") as login_file:
    try:
        users = json.load(login_file)
    except:
        users = {}

Recently, I'm doing a presentation for my code. However, my lecturer requires me to break down the code into pseudocode.

I can't find any pseudocode terms that fit in the with statement. I need to find alternative solution that can replace the with statement above.

 #i suppose it should look like this:...
def dummyname(login_file):
    login_file = process open("login_data.txt","r")
    while
        users != {}
    do
        users = process json.load(login_file)
process dummyname(login_file)
#is it something like this?
2
  • 1
    Top tip: bare except: is very bad practice. Commented Sep 28, 2015 at 7:44
  • @jonrsharpe except ValueError: should work right? Commented Sep 28, 2015 at 10:55

2 Answers 2

3

If you don't mind to write less safe pseudo-code ( and write safe after ) you could open-close.

login_file = open "login_data.txt" in text reading mode
users = load_json( login_file )
if load_json failed,
    users = {}
close( login_file )
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of replacing with statements, describe what is going on in pseudocode. Context managers are fundamental programming elements.

1 Comment

i'll try my best to understand it~ Coz sometime i know how to use a code but without knowing how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.