Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving information
#1
So totally brand new to Python, and I thought a good first program would be to write a simple login system. My issue is I don't know how to get python to save the information that the user inputs. In other words, after the user runs the program, how can I get Python to save that information for another time, instead of executing the program from the beginning. Sorry if that was hard to understand, I haven't quite got the terminology down yet.
Thanks.
Reply
#2
If you're talking about login from a web application, there are multiple examples available for that
for example part 5 of this tutorial: https://blog.miguelgrinberg.com/post/the...ins/page/0
If you choose to use this I suggest you start with part 1 and proceed through part 5.
If not a web application, you may want to read up on:
getpass: https://docs.python.org/3.8/library/getpass.html
also look through: https://pypi.org/search/?q=password
Reply
#3
Lots of different ways to store information. Since this is your first program, to keep it simple you may wish to save the information in plain text in a file and worry about security later. See keywords with, open, read, and write. As you get more experience you may wish to look into JSON, Pickle, and SQLite3.
Reply
#4
Okay so I know this code will look really basic and have a bunch of stuff that may be unnecessary/un-efficient, but keep in mind it is my first program. I have another python file named "User" and a text file called "passwords." I'm trying to save the passwords and usernames that the user inputs to the text file in the code at the bottom. Instead I get this: <User.User object at 0x10ae8b7f0>

from User import User
select = input("Enter login/register: ")


def password():
    password = ""
    return password

def username():
    username = ""
    return username

user1 = User(password, username)

if select == "login":
    username = input("Username: ")
    password = input("Password: ")
elif select == "register":
    def translate(password):
        translation = ""
        for letter in password:
            if letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890":
                if letter.isupper():
                    translation = translation + "*"
                else:
                    translation = translation + "*"
            else:
                translation = translation + letter
        else:
            for letter in password:
                if letter in "`~!@#$%^&*()_-+=\|{}[];':,./<>?":
                    print("Error: invalid password character")
        return translation

    username = input("Please make a username: ")
    print("Your password may contain any combination of uppercase and lowercase letters and numbers.")
    password = print(translate(input("Please make a password: ")))
else:
    print("invalid selection.")



appendMe = user1
appendFile = open("passwordsFile.txt)", "a")
appendFile.write(str(appendMe))
appendFile.close



class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password
The class User code is from the file User.
Reply
#5
user1 is an object. You assign that object a second reference, appendMe, then get the string representation of that object, which is is what you see saved to the file.
Instead try appendFile.write(appendMe.password)
Reply
#6
update: So my computer literally died so I haven't been able to respond or work on the code till I got a new one, but now when I program and make a username the text save in a new file labeled "passwordsFile.txt" and it looks something like this: <function username at 0x10c2358b0>
Reply
#7
Show your current code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
its the same as before but with jefsummers suggestion
Reply
#9
I see scope issues all over this. You have a function named password that has a variable password, and you have variables named password in your main code.

Also, you define a function inside an if statement. Is the function undefined if the criteria are not met? Bad idea.

The else in line 29 is connected to which if?

You define the order in the class User definition username followed by password. When you create a class instance in line 13 it is password, username.

In line 37 you define password as a print statement. That is why you get a function.

No idea where or why the import of User.

There is an extra parenthesis in line 44

This gets you a lot closer, but your passwords will all be just strings of stars....
class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

def translate(password):
    translation = ""
    for letter in password:
        if letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890":
            if letter.isupper():
                translation = translation + "*"
            else:
                translation = translation + "*"
        else:
            translation = translation + letter
    return translation

select = input("Enter login/register: ")
 
user1 = User('','')

if select == "login":
    username = input("Username: ")
    password = input("Password: ")
    user1 = User(username, password)
elif select == "register":
    username = input("Please make a username: ")
    print("Your password may contain any combination of uppercase and lowercase letters and numbers.")
    password = translate(input("Please make a password: "))
    user1 = User(username,password)

else:
    print("invalid selection.")
print(user1.username,user1.password) 
appendMe = user1.password
appendFile = open("passwordsFile.txt", "a")
appendFile.write(str(appendMe))
appendFile.close
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020