0

I'm wanting to store values in a file so that they maintain their properties. Please see a snippet of my code below.

        if selection == ("1"):
            newUserID = input ("Please enter their 3 digit ID: ")
            #check the user has put in numbers and at least 3 digits
            newUserName = input("Please enter the employees name: ")
            #check for string not number. Has to be exactly their name
            newUserPassword = input ("Please enter their Clocking in password: ")
            count = count + 1
            uniqueUserID = (newUserID, newUserName, newUserPassword, count)
            uniqueUserID = str(uniqueUserID)
            file.write(uniqueUserID)
            file.close()

I'm using Python and also need to make it so that the user's inputs are stored in a "count" variable to keep track of how many users there are.

Thanks.

1 Answers1

0

Put them values in a dict like below and then write the dictionary as json in the file.

dict = {}
dict['newUserID'] = newUserID
dict['newUserName'] = newUserName
dict['newUserPassword'] = newUserPassword
dict['count'] = count
with open('data.json', 'w') as fp:
    json.dump(dict, fp)

This assumes import json

MadaManu
  • 868
  • 4
  • 10