在pycharm中运行但不在控制台中运行的程序

时间:2019-12-16 21:47:10

标签: authentication passwords

因此,我正在制作一个登录系统,能够创建用户名并传递用户名,然后将其写入文件,然后再次运行时加载这些文件(使用pickle,用过2个原因无法弄清为什么不这样做)。加载两个列表)

但是我似乎无法弄清的主要问题是,在控制台上运行它只是在密码创建阶段就关闭了。

但是在编辑器(pycharm)中运行却可以。只是在寻求建议。为什么如果我写入相同的pickle文件,它将无法正确回读,以及为什么它在控制台中崩溃。我错过了什么吗?

#import os to use cls feature
#import time to use the sleep feature
#import pickle to save and read pickle files
import os,time
import pickle

# log in function
def Login():
    print("**Login Systems****")
    print("")
    usertry = input ("Please Enter your user name = ").lower()
    if usertry in usernames:
        # checks if in list
        positioncheck = usernames.index(usertry)
        #print(positioncheck)
        number_of_tries = 3
        password_correct = False
        while number_of_tries > 0 and not password_correct:
            # number of tried will go down if password is wrong
            passtry = input("Please enter your password = ")
            # position of password is checked on list to see if matches username position
            if passtry in passwords:
                positioncheck2 = passwords.index(passtry)
                print(positioncheck2)
                if positioncheck == positioncheck2:
                    os.system("cls")
                    print("**Barraclough Systems****")
                    print("")
                    print("Password accepted Log In Sucessfull")
                    password_correct = True
                    time.sleep(10)
                else:
                    print("Username and password miss match ")
                    number_of_tries -= 1
                    # reduces the number of tries remaining
            else:
                print("Password is incorrect.")
                number_of_tries -= 1
            if number_of_tries == 0:
                print('too many tries - exiting program')
                exit()
    # user is asked if they would like to retry
    elif usertry not in usernames:
        print("This is not a valid username ")
        time.sleep(2)
        again = input("Would you like to try again ? ( Y or N ) " ).lower()
        if again == "y":
            os.system("cls")
            Login()
        else:
            print("Returning to Main menu : ")
            time.sleep(2)
            os.system("cls")

# username creation Function
def NewUser():
    print("**Login Systems****")
    print("")
    print("TIP: If username taken try using nickname for first name ")
    print("")
    GenUser = ""
    #validility checks set to false for checking if entry was valid
    valid_first = False
    valid_last = False
    # if entry with less no characters are detected error given.
    while not valid_first:
        FirstName = input("Please enter your First Name = ").lower()
        if len(FirstName) < 1:
            print ("No entry was detected. Please try again")
        else:
            valid_first = True
    # if entry with less no characters are detected error given.
    while not valid_last:
        LastName = input("Please Enter your Last Name = ").lower()
        if len(LastName) < 1:
            print("No entry was detected. Please try again")
        else:
            valid_last = True
    # the first 3 characters are taken from first and last name strings to create a new shorter one for the username
    First3 = FirstName[0:3]
    Last3 = LastName[0:3]
    GenUser = First3 + Last3
    if GenUser in usernames:
        print("This user name is taken please retry ")
        time.sleep(3)
        os.system("cls")
        NewUser()
    # username has been created and then added to the list
    else:
        usernames.append(GenUser)
        print("Your Generated Username is", GenUser)
        print("\n\nPlease Take Note of this username, You will next be asked to create a password ")
        print("")
        input ("\nPress enter to continue")
        os.system("cls")
        CreatePass()

# password creation function.
def CreatePass():
    valid_password = False
    while not valid_password:
        valid_password = True
        # to reset at the start of each loop

        print("**Login Systems****")
        print("\nYou password must contain :")
        print("1 Upper Case Character")
        print("1 Lower Case Character")
        print("1 Number")
        print("1 Special Symbol from the following ! £ $ % & @ # ? ")
        print("Must be between 8 - 16 characters in length\n")
        print("")
        Symbols = ["!", "£", "$", "%", "&", "@", "#","?"]
        # creation of symbols to allow checking
        password = input("Enter a password = ")

        # password will then go through validation checks
        if len(password) < 8 or len(password) > 16:
            print ("\npassword needs to be between 8 and 16 characters")
            valid_password = False

        if not any(char.isupper() for char in password):
            print ("\nPassword needs an upper case character.")
            valid_password = False

        if not any(char.islower() for char in password):
            print ("\nPassword requires a lower case character.")
            valid_password = False

        if not any(char.isdigit() for char in password):
            print ("\nPassword requires a number.")
            valid_password = False

        if not any(char in Symbols for char in password):
            print ("\nPassword requires a special character (! £ $ % & @ # ? ). Please try again\n ")
            valid_password = False
        # If valid the password will be added to the list
        if valid_password:
            passwords.append(password)
            pickle.dump(usernames, open("names.pck", "wb"))
            pickle.dump(passwords, open("pass.pck", "wb"))
            print("\nYour Password Has been Accepted\n")
            print("You will now be redirected to the menu where you can log in")
            time.sleep(4)
            os.system("cls")
        else:
            print('\n\nPlease try again\n\n')

# just so i can see if the passwords and username's had saved to the list
def testing():
    print(usernames)
    print(passwords)



# my 2 lists for the username and passwords
# followed by a pre-set varible
usernames = []
passwords = []
selection = 99

try:
    usernames = pickle.load(open("names.pck", "rb"))
    passwords = pickle.load(open("pass.pck", "rb"))
except:
    print()

# menu selection, a few print statements in an if statement calling a defined function
while selection != "0":
    print("**Login Systems****\n")
    print("1. Login")
    print("2. Create New user")
    print("3. PRINT USER NAMES AND PASSWORDS. TESTING PURPOSES ONLY \n")
    selection = input("Please select a menu option : ")
    os.system("cls")
    if selection == "1":
        Login()
    elif selection == "2":
        NewUser()
    elif selection == "3":
        testing()
    else:
        print()

0 个答案:

没有答案