尽管条件满足,循环仍然没有结束

2024-09-30 12:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经构建了一个代码来执行一个简单的任务:接收用户名和密码,检查它们是否满足条件,如果不正确则重复该过程,如果正确则停止程序。但是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)

Tags: yourifusernamepasswordelseplayerprintenter
2条回答

你只需要重新排列一下代码,试试下面的代码:

confirmation = 0
while confirmation != 4:
    confirm1 = 0
    confirm2 = 0
    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: ")
    confirmation = loginConfirmation (user1,user2,pass1,pass2,confirm1,confirm2)

注意:

1-两个变量confirm1和confirm2必须在每次while调用后重置为零

2-必须将函数“loginconconfirmation”的输出值赋给confirmation变量

您需要为变量confirmation指定函数loginConfirmation的返回值,以便中断while循环。你知道吗

在给定的代码中,变量confirmation的值总是0,因此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: ")
     confirmation = loginConfirmation (user1,user2,pass1,pass2,confirm1,confirm2)

相关问题 更多 >

    热门问题