gpt4 book ai didi

python - 为什么满足条件时我的函数不执行?

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:39 29 4
gpt4 key购买 nike

我正在尝试为我的服务器应用程序制作一个登录表单,但偶然发现了一个看不见的错误(最糟糕的错误)。我有一个 tries 变量,每当用户错误输入用户名时,该变量就会增加 1。当tries变量达到3时,程序应该执行block()函数。我的问题是事实并非如此。

我的所有 .txt 文件都是空白的,因此没有错误,一切看起来都很好。请帮忙,因为我正在拔头发!

代码如下:

import socket
import time
import sys
import os

tries = 0

def server():

def block():

#get host ip
ipBlock = socket.gethostbyname(socket.gethostname())

#create black list
ipBlackList = open("ipBlackList.txt", "w")
ipBlackList.close()

#read black list
ipBlackList = open("ipBlackList.txt", "r")
isBlocked = ipBlackList.readlines()
ipBlackList.close()

#disable account for 60 seconds
if ipBlock in isBlocked:
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print('Your account has been temporarily disabled. Please
try again in '+timer, end=" seconds.\r")
time.sleep(1)
t -= 1
os.remove('ipBlackList.txt')
print('\nYour account has been re-enabled. You may log in
now.')
time.sleep(2)
t = 60
countdown(int(t))
block()

print("Enter administrator login details: ")

def usernameLogin():
#global vars
global tries

if tries == 3:
#read black list
ipBlackList = open("ipBlackList.txt", "r")
isBlocked = ipBlackList.readlines()
ipBlackList.close()

#get host ip
ipBlock = socket.gethostbyname(socket.gethostname())

if ipBlock not in isBlocked:
ipBlackList = open("ipBlackList.txt", "w")
ipBlackList.write(ipBlock)
ipBlackList.close()
block()

username = input("\nUsername: ")

#check if username exists
usernames = open("usernames.txt", "r")
checkUser = usernames.readlines()
usernames.close()

if username not in checkUser:
print("That username doesn't exist. Please try again.")
tries += 1
print(tries)
time.sleep(2)
usernameLogin()

print("im in")
time.sleep(3)

usernameLogin()
server()

最佳答案

事实上,您的代码确实在 tries==3 时执行 block() 函数,但在 block() 的开头> 您重新创建黑名单文件,然后读取空文件,结果条件 if ipBlock in isBlocked: 始终为 false。

因此,我已将创建文件的命令置于 block() 之外。另外,我更改了 countdown() 函数中黑名单文件的 remove 并重新创建但不删除它,因为您的代码在重新启动时会尝试再次读取它询问用户名。另外,当 block() 结束时,我设置了 tries == 0 。此外,countdown() 末尾的缩进不正确。

import socket
import time
import sys
import os

tries = 0

def server():

def block():

#get host ip
ipBlock = socket.gethostbyname(socket.gethostname())

#read black list
ipBlackList = open("ipBlackList.txt", "r")
isBlocked = ipBlackList.readlines()
ipBlackList.close()

#disable account for 60 seconds

if ipBlock in isBlocked:
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print('Your account has been temporarily disabled. Please \
try again in '+timer, end=" seconds.\r")
time.sleep(1)
t -= 1

#os.remove('ipBlackList.txt')
# Recreate an empty black list file
ipBlackList = open("ipBlackList.txt", "w")
ipBlackList.close()

print('\nYour account has been re-enabled. You may log in \
now.')
time.sleep(2)
t = 6
countdown(int(t))

#create black list file (empty)
ipBlackList = open("ipBlackList.txt", "w")
ipBlackList.close()
block()

print("Enter administrator login details: ")

def usernameLogin():
#global vars
global tries

if tries == 3:
#read black list
ipBlackList = open("ipBlackList.txt", "r")
isBlocked = ipBlackList.readlines()
ipBlackList.close()

#get host ip
ipBlock = socket.gethostbyname(socket.gethostname())
if ipBlock not in isBlocked:
ipBlackList = open("ipBlackList.txt", "w")
ipBlackList.write(ipBlock)
ipBlackList.close()
block()

tries = 0

username = input("\nUsername: ")

#check if username exists
usernames = open("/test/usernames.txt", "r")
checkUser = usernames.readlines()
usernames.close()

if username not in checkUser:
print("That username doesn't exist. Please try again.")
tries += 1
print(tries)
time.sleep(2)
usernameLogin()

print("im in")
time.sleep(3)

usernameLogin()
server()

关于python - 为什么满足条件时我的函数不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921261/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com