【问题标题】:While loop not ending despite fulfilling conditions尽管满足条件,但循环没有结束
【发布时间】:2019-02-27 14:12:41
【问题描述】:

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

【问题讨论】:

  • 您永远不会将loginConfirmation 的输出分配给确认。试试confirmation=loginConfirmation (user1,user2,pass1,pass2,confirm1,confirm2)
  • (问题在被回答时不会在此处关闭。仅当它们被视为离题或无法回答时才关闭)。
  • 我知道这个话题不是封闭的。我只是明确表示问题已解决

标签: python python-3.x while-loop


【解决方案1】:

您需要将函数loginConfirmation 的返回值分配给变量confirmation,以中断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)

【讨论】:

    【解决方案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- 您必须将函数“loginConfirmation”的输出值分配给确认变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2019-12-17
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      相关资源
      最近更新 更多