我已经构建了一个代码来执行一个简单的任务:接收用户名和密码,检查它们是否满足条件,如果不正确则重复该过程,如果正确则停止程序。但是while循环不会停止,程序会继续运行。我在下面插入了代码。当条件满足时,如何使while循环停止?你知道吗
def loginConfirmation(user1,user2,pass1,pass2,confirm1,confirm2):
if len(user1) > 6:
confirm1 = confirm1 + 1
else:
print("Invalid username Player 1")
if len(user2) > 6:
confirm2 = confirm2 + 1
else:
print("Invalid username Player 2")
if pass1 == ("password"):
confirm1 = confirm1 + 1
else:
print("invalid passsword Player 1")
if pass2 == ("password"):
confirm2 = confirm2 + 1
else:
print("Invalid passsword Player 2")
confirmation = confirm1 + confirm2
return confirmation
confirmation = 0
confirm1 = 0
confirm2 = 0
while confirmation != 4:
print("Please enter your details below. Usernames must be at least six letters long.")
user1 = input("Player 1, enter your username: ")
pass1 = input("Player 1, enter your password: ")
user2 = input("Player 2, enter your username: ")
pass2 = input("Player 2, enter your password: ")
loginConfirmation (user1,user2,pass1,pass2,confirm1,confirm2)
你只需要重新排列一下代码,试试下面的代码:
注意:
1-两个变量confirm1和confirm2必须在每次while调用后重置为零
2-必须将函数“loginconconfirmation”的输出值赋给confirmation变量
您需要为变量
confirmation
指定函数loginConfirmation
的返回值,以便中断while循环。你知道吗在给定的代码中,变量
confirmation
的值总是0,因此while循环的中断条件永远不会满足。你知道吗使用以下内容更新代码:
相关问题 更多 >
编程相关推荐