Fady Esam
Fady Esam

Reputation: 31

Storing items in a module-level list

How can I save the items in lists because when using Sign_in function, it prints "invalid" not "success"?

import lists_module

def Sign_in():
    email = input("Enter Your Email").strip()
    password = input("Enter your password").strip()
    if email in lists_module.emails and int(password) in lists_module.passwords:
        print("success")
    else : print("invalid")

def Sign_up():
    first_name = input("Enter Your First Name").strip().capitalize()
    last_name = input("Enter Your Last Name").strip().capitalize()
    new_email = input("Enter Your Email").strip().capitalize()
    new_password = input("Enetr New Password").strip().capitalize()
    lists_module.Fname.append(first_name)
    lists_module.Lname.append(last_name)
    lists_module.emails.append(new_email)
    lists_module.passwords.append(new_password)
    print("Sign-In Page")
    Sign_in()

Note: Fname and Lname and email are empty lists in another module.

Upvotes: 0

Views: 90

Answers (2)

martineau
martineau

Reputation: 123453

The reason you keep getting "invalid" is because you don't process the user's input consistently in the Sign_up() and Sign_in() functions. One way to update the lists in the lists_module is by using the atexit module to register a function that will be executed when the script ends.

import atexit
import lists_module


def Sign_in():
    email = input("Enter Your Email ").strip().capitalize()
    password = input("Enter your password ").strip().capitalize()

    if email in lists_module.emails and password in lists_module.passwords:
        print("success")
    else:
        print("invalid")

def Sign_up():
    first_name = input("Enter Your First Name ").strip().capitalize()
    last_name = input("Enter Your Last Name ").strip().capitalize()
    new_email = input("Enter Your Email ").strip().capitalize()
    new_password = input("Enetr New Password ").strip().capitalize()
    lists_module.Fname.append(first_name)
    lists_module.Lname.append(last_name)
    lists_module.emails.append(new_email)
    lists_module.passwords.append(new_password)

    print("Sign-In Page")
    Sign_in()

def update_lists_module():
    with open('lists_module.py', 'w') as outp:
        for name in dir(lists_module):
            if not name.startswith('_'):
                value = getattr(lists_module, name)
                outp.write(f'{name} = {value!r}\n')


atexit.register(update_lists_module)
Sign_up()

Upvotes: 0

Game Developement
Game Developement

Reputation: 409

The appended values are just temporary. Use file I/O for permanent changes. I.e.:

def Sign_in():
    email = input("Enter Your Email").strip()
    password = input("Enter your password").strip()
    emails = open('emails.txt','r').readlines()
    passwords = open('passwords.txt','r').readlines()
    if email in emails and password in passwords:
        print("success")
    else : print("invalid")


def Sign_up():
    first_name = input("Enter Your First Name").strip().capitalize()
    last_name = input("Enter Your Last Name").strip().capitalize()
    new_email = input("Enter Your Email").strip()
    new_password = input("Enetr New Password").strip()
    open('fnames.txt','a+').write(first_name+'\n')
    open('lnames.txt','a+').write(first_name+'\n')
    open('emails.txt','a+').write(new_email+'\n')
    open('passwords.txt','a+').write(new_password+'\n')
    print("Sign-In Page")
    Sign_in()

This will write the values to files on your computer, then read them from those files, that way, when you run the prorgam a second time, the changes are permanent.

Upvotes: 1

Related Questions