Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2.7-登录尝试_Python_Login - Fatal编程技术网

Python2.7-登录尝试

Python2.7-登录尝试,python,login,Python,Login,下面是简单的登录脚本 我有小虫子 在文本文件中写入: name :test password :123321 admin :0 我想做: if password and username exist then #do code else #do code 您的问题是缩进-您希望将Log=中的所有内容缩进到while循环中 import os import sys print "Hello to login - login(username,password

下面是简单的登录脚本

我有小虫子

在文本文件中写入:

name :test    
password :123321    
admin :0
我想做:

if password and username exist then 
    #do code
else
    #do code

您的问题是缩进-您希望将Log=中的所有内容缩进到while循环中

import os
import sys

print "Hello to login - login(username,password)"
login = 0
att = 1
while login == 0:

#Check if user login/exist

    Log = raw_input("Enter username: ")
    if os.path.isfile(Log + ".txt"):
        userfile = (Log+".txt")
        f = open(userfile,"r")
        Pass = raw_input("enter password: ")
        Lines = f.readlines()
        Password1 = Lines[1].split(":")
        Passwordl = Lines[1].strip()
        if Passwordl[10:] == Pass:
            login = 1
            break
    elif att == 3:
        print "you try to log in more then 3 time, user locked"
        break
    else:
        print "username not exist or pass wrong"
        att += 1

告诉我们问题是,else只是在第一个if中被执行,也就是说,如果密码错误,它将返回到loop。你应该使用计数器或其他东西,还应该使用while循环。不是if-else循环
import os
import sys

print "Hello to login - login(username,password)"
login = 0
att = 1
while login == 0:

#Check if user login/exist

    Log = raw_input("Enter username: ")
    if os.path.isfile(Log + ".txt"):
        userfile = (Log+".txt")
        f = open(userfile,"r")
        Pass = raw_input("enter password: ")
        Lines = f.readlines()
        Password1 = Lines[1].split(":")
        Passwordl = Lines[1].strip()
        if Passwordl[10:] == Pass:
            login = 1
            break
    elif att == 3:
        print "you try to log in more then 3 time, user locked"
        break
    else:
        print "username not exist or pass wrong"
        att += 1