LeCarloVC
LeCarloVC

Reputation: 109

Storing and re-importing usernames and passwords in a text file

I'm a student learning Python, new to coding in general. I've started this "minigame" side project, but I'm having trouble creating a script that creates usernames and passwords and stores them into a text file.

def createUser():
   import Usernames_Passwords.txt
   open("Usernames_Passwords.txt", "r")
   Username = raw_input("Create a username: ")
   Password = raw_input("Create a password: ")

But it comes up with the error:

import Usernames_Passwords.txt
ImportError: No module named Usernames_Passwords.txt

I removed the import line, and the rest of the program works up until I get this error:

if User == Username and PassWord == Password:
NameError: global name 'Username' is not defined

This is the entire script:

import os
import time
import sys


def createUser():
import Usernames_Passwords.txt
open("Usernames_Passwords.txt", "r")
Username = raw_input("Create a username: ")
Password = raw_input("Create a password: ")



def login():

    User = raw_input ("Enter Username: ")
    time.sleep(0.5)
    PassWord = raw_input ("Enter Password: ")

    if User == Username and PassWord == Password:
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "Login successful! Proceeding"
        logged()

    else:
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "."
        time.sleep(0.5)
        print "Your Username or Password did not match, please try again"
        time.sleep(2)

def logged():

print "WELCOME TO SUPER MARIO INTERACTIVE SIMULATOR 2000"
time.sleep(1.5)
print "PLEASE ENTER YOUR NAME BELOW"
Name = raw_input ("NAME: ")
print "HELLO THERE", Name
time.sleep(1)
print "PLEASE ALSO INSERT YOUR AGE"
Age = raw_input ("AGE: ")
print "GOOD YOU ARE", Age
time.sleep(2)
print "BUT FIRST YOU MUST PASS THIS TEST"

createUser()
time.sleep(3)
login()

Upvotes: 2

Views: 731

Answers (2)

Casey P
Casey P

Reputation: 140

There are three major issues I see with the code you posted:

  1. You are opening a read only copy of your text file. As MoonMoon pointed out you don't need that import statement. Import is just for libraries you want to use, so just get rid of that (this is the cause of the first error, python can't find a module by the name of "Usernames_Passwords.txt"). You want to use the 'open' function to access a file. The second argument of the open() function is the mode you are opening the file in. "r" is read, "w" is write, and "a" is append. Because I imagine that you want to run this multiple times and keep all of the past usernames and passwords in the same file you want append. You are also gonna need to close the file when you are done. The easiest way to do that is with a with statement. You can read more about the with statement here:

    with open("Usernames_Passwords.txt", 'a') as f:
        # do something 
    
  2. You aren't writing anything to your txt file. You have to tell python to write to the file or you aren't going to save anything. Try something like this:

    with open("Usernames_Passwords.txt", 'a') as f:
        Username = raw_input("Create a username: ")
        Password = raw_input("Create a password: ")
        f.write('%s, %s \n' % (Username, Password))
    

    if what I did with the % confused you its called the string format operator. It allows you to insert variables into strings with a specified format. A little googling will tell you everything you need to know about that.

  3. You aren't checking the Username_Passwords.txt in your login function. This is the cause of the second error you mentioned. When you define Username in another function, that creates a "local" variable. That means you can only access it in that function. Check out this python tutorial it does a great job explaining what global and local variables are. What I think you want to do is set username and passsword in your login function by reading from the file you wrote to earlier (this means you want to use open() in the "read" mode).

I'll leave it to you to figure out the particulars, but that's a great place to start. There are also some issues in the indentions in the code you posted, but I think those were just copying errors judging from the error you recieved.

BTW, when I was first learning coding, I used "Code Academy". It's an online interactive tutorial that teaches python. I think you would get a lot out of it.

Upvotes: 1

MoonMoon
MoonMoon

Reputation: 19

When creating a new file you need to use the "w" write mode written like so.

file = open("Usernames_Passwords.txt", "w")

if the file is already created and you would like to add to it you can use the append mode written like this.

file = open("Usernames_Passwords.txt", "a")

if you want to read the files from a file that is already written you may use the "r" mode like you did before.

file = open("Usernames_Passwords.txt", "r")

the import statement is used for calling other libraries as far as I know so you dont need that for this program in particular.

Upvotes: 1

Related Questions