因此,在上下文中,我试图编写一个脚本来检查文本输入是否满足用户名的所有必要要求。
但是,即使满足条件,在我写的要重复执行该功能的部分也总是在第一次尝试后重复执行。第一次尝试会按预期进行。
此部分使用不满足条件的列表来确定是否重复该功能,但是,即使用户名正确,也可以在第一次尝试后重复执行。
if error_stack != []:
repeat = True
else:
repeat = False
return repeat
repeat = username_input(input("Please enter a username with only numbers and letters, which is above 2 characters and below 15 characters. If the username is taken you will be asked to pick a new one. \n|:"))
print(repeat) #please help its always true.....
while repeat == True:
repeat = username_input(input("Please enter a username with only numbers and letters, which is above 2 characters and below 15 characters. If the username is taken you will be asked to pick a new one. \n|:"))
print(repeat) #please help its always true.....
if __name__ == "__main__":
main()
在此屏幕快照中,我输入1个用户名,这将导致控制变量设置为True,然后重复循环,然后输入1个用户名,这将导致控制变量设置为False,并且循环不重复。 >
在此屏幕快照中,我输入了1个用户名,这将导致控件var设置为False,并且循环不会重复。然后它将按预期工作。
有人可以第一次向我解释为什么条件只能按其应有的功能运行(据我所知,每次循环结束后,每次循环结束时都应检查条件是否满足)。退出吗?
编辑:部分的完整代码
import os
STANDARD_CHARS = list("abcdefghijklmnopqrstuvwxyz1234567890 ")
ERROR_DICT = {
"NonStandardCharacterError":"Your username should only have letters and numbers in it, please pick another.",
"CurrentUserError":"Your username has already been taken, please pick another.",
"LengthError":"Your username should be more than 2 characters in length, and fewer than 15 characters in length, please pick another.",
}
def main():
printed_errors = []
error_stack = []
current_users = []
def username_input(word_input):
for char in list(word_input.lower()):
if char not in STANDARD_CHARS:
error_stack.append("NonStandardCharacterError")
if word_input.lower() in current_users:
error_stack.append("CurrentUserError")
if len(word_input) > 15 or len(word_input) < 3:
error_stack.append("LengthError")
for error in error_stack:
try:
if error not in printed_errors:
print(ERROR_DICT[error])
printed_errors.append(error)
except KeyError:
print("Your username has thrown an unknown error , please try again later or pick another username.")
if error_stack != []:
repeat = True
else:
repeat = False
return repeat
repeat = username_input(input("Please enter a username with only numbers and letters, which is above 2 characters and below 15 characters. If the username is taken you will be asked to pick a new one. \n|:"))
print(repeat)
while repeat == True:
repeat = username_input(input("Please enter a username with only numbers and letters, which is above 2 characters and below 15 characters. If the username is taken you will be asked to pick a new one. \n|:"))
print(repeat)
if __name__ == "__main__":
main()
答案 0 :(得分:2)
您在error_stack
中将[]
初始化为main
,并在username_input中附加到其后。但是,在初始创建之后,您再也不会将其重置为空列表。因此error_stack
越来越多,即使用户输入有效的输入,也保留了旧的错误。
尝试在error_stack
内部而不是username_input
内部创建main
。然后,它将为每个新的用户输入提示设置为空列表。
def main():
printed_errors = []
current_users = []
def username_input(word_input):
error_stack = []
for char in list(word_input.lower()):
#...
如果出于某种原因有必要将error_stack
保留在较高的范围内,则可以使用clear
方法将其清除。
def main():
printed_errors = []
error_stack = []
current_users = []
def username_input(word_input):
error_stack.clear()
for char in list(word_input.lower()):
#...