所以我有一个学校项目的代码(它解释了变量名称),但是当我到达底部的for循环时,它表示列表索引超出了范围。它应该输入用户名,然后在要求输入密码之前检查它。我试图弄清楚什么是错的,但没有任何作用。
def passwordSystem():
#TASK ONE
username_array = []
password_array = []
for i in range (1, 30):
username_to_be_entered = str(input("Enter a username"))
acceptable_password = False
while not acceptable_password:
password_to_be_entered = str(input("Enter a password between 6 and 12 characters"))
if len(password_to_be_entered) > 12 or len(password_to_be_entered) < 6:
print ("Your password was not between 6 and 12 characters")
else:
acceptable_password = True
print ("Password accepted")
print ("Your username is:" ,username_to_be_entered)
print ("Your password is:" ,password_to_be_entered)
#TASK TWO
username_accepted = False
while not username_accepted:
username_to_be_checked = str(input("Please enter your username"))
for i in range (1, 30):
if username_array[i] == username_to_be_checked:
print ("Username accepted")
username_accepted = True
答案 0 :(得分:1)
你的名单永远是空的。在此代码块中,您需要将用户名和密码附加到各自的列表中。
while not acceptable_password:
password_to_be_entered = str(input("Enter a password between 6 and 12 characters"))
if len(password_to_be_entered) > 12 or len(password_to_be_entered) < 6:
print ("Your password was not between 6 and 12 characters")
else:
acceptable_password = True
username_array.append(username_to_be_entered)
password_array.append(password_to_be_entered)
print ("Password accepted")
然后,要检查用户名,您应该使用in
关键字,而不是手动迭代整个列表。
#TASK TWO
username_accepted = False
while not username_accepted:
username_to_be_checked = str(input("Please enter your username"))
if username_to_be_checked in username_array:
print ("Username accepted")
username_accepted = True