尽管满足条件,但循环未结束

时间:2019-02-27 14:12:41

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

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

2 个答案:

答案 0 :(得分:0)

您需要将函数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)

答案 1 :(得分:0)

您只需要重新排列一下代码,请尝试以下代码:

<span class="article-price">
    <span>
        <span>149,99</span>
        <span>€</span>
    </span>
</span>
<div>your installment is <span id="installment"></span></div>

NB:

1-每次通话后,两个变量Confirm1和Confirm2必须重置为零

2-您必须将函数'loginConfirmation'的输出值分配给确认变量