简体   繁体   English

在特定条件下跳出循环或继续循环(Python)

[英]Breaking out of a loop or continuing it under certain conditions (Python)

I'm a little new to Python so I apologise in advance if this is a really simple question but I have been trying to wrap my mind around this problem for a while now.我对 Python 有点陌生,所以如果这是一个非常简单的问题,我提前道歉,但我一直在努力解决这个问题一段时间。

Simply put, my code is to prompt the user if they have written down a couple of values correctly, If they have I want the loop to continue so the values can be saved to a database.简单地说,我的代码是提示用户他们是否正确地写下了几个值,如果他们有,我希望循环继续,以便将值保存到数据库中。 If they have not I would like the code to loop back to the top of the while loop so they can re-enter the values.如果他们没有,我希望代码循环回到 while 循环的顶部,以便他们可以重新输入值。

For some reason when I enter 'no' It goes through the loop regardless, how may I fix this?出于某种原因,当我输入“否”时,它无论如何都会通过循环,我该如何解决这个问题? Here's the code below:下面是代码:

while True:

  clear()

  print("\nPlease input the required data.\n")

  in_name = input('Name: ')
  in_email = input('Email: ')
  in_address = input('Address: ')
  
  clear()

  print("\nDoes this look correct?\n")
  
  print("#--START--#\n")
  print("Name: " + in_name)
  print("Email: " + in_email)
  print("Address " + in_address)
  print("\n#---END---#\n")

  validate == input(">>> ")

  if validate == "no":
    continue

  elif validate == "yes":
    print("/nAttempting to copy to the database...")

  cursor.execute("""
  INSERT INTO contacts(name, email, address)
  VALUES (?,?,?)
  """, (in_name, in_email, in_address))

  conn.commit ()

  print ( 'Data entered successfully.\n' )

  break

(I should note that this write program is part of a larger program, the loop is nested within another loop that acts as the main menu, perhaps that may help.) (我应该注意,这个编写程序是一个更大程序的一部分,循环嵌套在另一个充当主菜单的循环中,也许这可能会有所帮助。)

the keyword continue will take you back to the next iteration without finishing the current one, example.关键字 continue 将带您回到下一个迭代,而无需完成当前迭代,例如。

for i in range(5):
if i == 2:
    continue
print(i * 5)

This means that when i is 2 it wont print(i=2 * 5), instead it will go up to start the next loop where i=3.这意味着当 i 为 2 时,它不会打印(i=2 * 5),而是会 go 开始下一个 i=3 的循环。 The output will be 0 5 15 20 output 将为 0 5 15 20

If you use break, it will just completely stop and exit out of the iteration once it reaches this keyword.如果你使用break,一旦到达这个关键字,它就会完全停止并退出迭代。 I think you're looking for the keyword pass.我认为您正在寻找关键字通行证。

'while True:' will always be True, meaning the loop will go on forever. 'while True:' 将始终为 True,这意味着循环将永远开启 go。 I believe that you'll need to break out of the loop for it to stop.我相信您需要跳出循环才能停止。 Based on Jorge Alvarez's answer, even changing 'continue' to 'pass' will allow the loop to go forever.根据 Jorge Alvarez 的回答,即使将“继续”更改为“通过”也将永远允许循环到 go。

If I understand correctly, you'll need to change the initial 'while True:' statement to test whether validate equals 'no' or 'yes'.如果我理解正确,您需要更改初始的“while True:”语句以测试 validate 是否等于“no”或“yes”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM