简体   繁体   English

我如何让这个简单的python登录程序循环?

[英]How can I get this simple python login program to loop?

this is my first time posting on stack overflow and I would be very appreciative if someone could help me with a simple loop function. 这是我第一次发布有关堆栈溢出的信息,如果有人可以通过简单的循环功能帮助我,我将不胜感激。 This project is supposed to be for a group of 4 people, however I have been majorly let down and have had no support from my team. 这个项目原本是一个4人一组的项目,但是我却大失所望,没有团队的支持。

Im a complete beginner to python and would be grateful for any help or suggestions. 我是python的完整入门者,非常感谢您的帮助或建议。

I have tried to mess about and incorporate a loop but I feel very out of my depth. 我试图弄乱并合并一个循环,但是我感到非常不了解。

The current program works and extracts the username and password in the .txt file as the format (username,password) however, it is the beginning of a quiz program. 当前程序可以工作,并以.txt文件格式(用户名,密码)提取用户名和密码,但是,这是测验程序的开始。 If an incorrect username/password is entered, the user is still able to begin the quiz. 如果输入了错误的用户名/密码,则用户仍然可以开始测验。

login_username = input('username: ')
login_password = input('password: ')
found_username = False

with open('passwords.txt', 'r') as password_file:
    for line in password_file:
        username, password = line.strip().split(',')

        if login_username == username:
            found_username = True
            if login_password == password:
                print('success!')
            else:
                print('login failure!')
            break

if not found_username:
    print('username invalid')

I would be extremely grateful for any support available :) 如有任何支持,我将不胜感激:)

You have a few different options. 您有几种选择。 The most basic of which will keep asking for a password, as long the correct one is not already entered. 只要尚未输入正确的密码,最基本的密码就会一直询问。 This also means making minimal edits to the code you already have. 这也意味着对现有代码进行最少的编辑。

logged_in = False #Defines if user is logged in or not
while not logged_in:
    login_username = input('username: ')
    login_password = input('password: ')
    found_username = False

    with open('passwords.txt', 'r') as password_file:
        for line in password_file:
            username, password = line.strip().split(',')

            if login_username == username:
                found_username = True
                if login_password == password:
                    print('success!')
                    logged_in = True #We're not logged in, so loop will exit
                else:
                    print('login failure!')
                break

    if not found_username:
        print('username invalid')

This will keep repeating to ask for the username AND password , so a better version might be to take the username (and username check) into a different while loop. 这将不断重复询问usernamepassword ,因此更好的版本可能是将username (和用户名检查)放入不同的while循环中。

Hope this helps! 希望这可以帮助!

First, I will input all the username and password into a dictionary Second, I use a while loop to keep asking the user to put into the correct username and password 首先,我将所有用户名和密码输入到词典中。其次,我使用while循环不断要求用户输入正确的usernamepassword

found_username = False
user_dict = {}
#Add username and password into a user dictionary (or library)
with open('passwords.txt', 'r') as password_file:
    for line in password_file:
        username, password = line.strip().split(',')
        user_dict[username] = password
while found_username == False: #keep asking until correct username and password
    login_username = input('username: ')
    login_password = input('password: ')
    #Check input username and password
    for user in user_dict:
        if login_username == user: #check username
            no_username_found = False
            if login_password == user_dict[user]: #check password
                print('success!')
                found_username = True  #accept
                no_username_found = False
                break   #stop when password is correct
            else:
                print('login failure!') #wrong password
                found_username = False
                break  #stop when password is wrong
        else:
            no_username_found = True    

    if no_username_found == True: #check if usename is not found
        print('username invalid') 

print(found_username)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM