我正在尝试完成一些Python代码,但是遇到了一些问题。基本上,我试图提示用户输入所需的用户名和密码以及喜欢的颜色(作为安全性问题),以在输入信息后初始化的GUI中使用。用户名和密码必须满足某些要求,您可以在代码的打印语句中找到这些要求。如果不满足要求,则会通过while循环继续提示用户输入信息,直到满足所有要求为止,而while循环将在满足条件时中断。我遇到的问题是,无论是否满足要求,循环都将结束,GUI仍将启动。看来valid_pass函数已被完全忽略。我是Python的新手,所以我并不了解。
def valid_user():
if (8 <= len(user) <= 24):
valid_user == True
print("Username meets requirements!")
else:
valid_user == False
print("Please make sure your username meets length requirements. Usernames must be in between 8 and 24 characters in length.")
def valid_pass():
if (8 <= len(pw) <= 24):
if pw.isdigit() == True:
if pw.isalpha() == True:
valid_pass() == True
print("Password meets requirements!")
else:
valid_pass() == False
print("Please make sure your password meets length requirements. Passwords must be in 8 and 24 characters in length and contain both letters and numbers.")
while (use_inp):
user = input("Set a username.")
pw = input("Now, select a password.")
user_answer = input("Select a favorite color")
valid_user()
valid_pass()
if valid_user() == False or valid_pass() == False:
continue
else:
break
break
答案 0 :(得分:0)
我已根据您的规范修改了您的代码,请告诉我它是如何工作的。谢谢!
def valid_user():
while True:
user = input("Set a username: ")
if (8 <= len(user) <= 24):
valid_user == True
print("Username meets requirements!")
break
else:
valid_user == False
print("Please make sure your username meets length requirements. Usernames must be in between 8 and 24 characters in length.")
def valid_pass():
while True:
pw = input("Now, select a password: ")
if (8 <= len(pw) <= 24):
if any(char.isdigit() for char in pw) == True and any(char.isalpha() for char in pw) == True:
print("Password meets requirements!")
break
else:
print("Password must contain alpha numeric and digits and be at least 8 length.")
else:
print("Please make sure your password meets length requirements. Passwords must be in 8 and 24 characters in length and contain both letters and numbers.")
user_answer = None
while user_answer == None:
valid_user()
valid_pass()
user_answer = input("Select a favorite color")
答案 1 :(得分:0)
我认为这里的功能是多余的。
usn_input: str = ''
pw_input: str = ''
colour_input: str = ''
valid_input_flag: bool = False
while not valid_input_flag:
valid_input_flag = True
usn_input = input('Enter a username: ')
pw_input = input('Enter a password: ')
colour_input = input('Enter your favorite colour: ')
if not (8 <= len(usn_input) <= 24):
valid_input_flag = False
print('Please make sure your username meets length requirements. Usernames must be in between 8 and 24 '
'characters in length.')
if not (8 <= len(pw_input) <= 24 and any(curr.isdigit() for curr in pw_input)
and any(curr.isalpha() for curr in pw_input)):
valid_input_flag = False
print('Please make sure your password meets length requirements. Passwords must be in 8 and 24 characters in '
'length and contain both letters and numbers.')
print(f'Valid inputs:\nUsername: {usn_input}\nPassword: {pw_input}\nColour: {colour_input}')
请注意,在进行任何验证之前,我会在之前获得全部3个输入。在某些情况下,最好先验证某个输入,然后再请求下一个。