Python 获取用户输入以停止while循环

Python 获取用户输入以停止while循环,python,python-3.x,Python,Python 3.x,我需要一种通过获取用户输入而不阻塞跨平台工作的循环本身来停止循环的方法 我尝试过的事情: 处理(关闭stdin,因此我无法使用input()) 线程(如果while循环终止并且我不再需要输入(),则无法终止线程) FLAG=False def break_main_loop():#如何执行此操作? 全球旗帜 用户_in=input() 如果用户_in==“停止”: FLAG=True 返回 其他: 断开主回路() def stuff(): 通过 def main(): #做一些事情,例如获取

我需要一种通过获取用户输入而不阻塞跨平台工作的循环本身来停止循环的方法

我尝试过的事情:

  • 处理(关闭stdin,因此我无法使用
    input()
  • 线程(如果while循环终止并且我不再需要
    输入()
    ,则无法终止线程)
FLAG=False
def break_main_loop():#如何执行此操作?
全球旗帜
用户_in=input()
如果用户_in==“停止”:
FLAG=True
返回
其他:
断开主回路()
def stuff():
通过
def main():
#做一些事情,例如获取其他用户输入()
尽管如此:
东西
如果标志:
打破
return#如果我返回这里,我需要取消break_main_循环的输入()
main()
试试这个:

class new_game:
    
    start_game = True
    # end_game = False

    def break_main_loop(self):  # how do I get this to execute?
        print("game is ending!")
    

    def stuff(self):
        print("this is stuff happening....now...game is: ", self.start_game)
        
    def game_loop(self):
        user_in = int(input("enter '2' to end game or '1' to keep playing:" ))
       
        if user_in == 1:
       
        
            self.stuff()
            self.game_loop()
        else:
            return self.break_main_loop()
 
Test_game = new_game()

Test_game.game_loop()

这将为您的工作和简单的使用。将主功能替换为此

def main():
  # do stuff, e.g. getting other user input()
  try:
     while True:
        stuff()
  except KeyboardInterrupt:
     print("Press 1 to quit")
 return  # if I return here, I need to cancel break_main_loop's input()

main()

我很乐意回答你的问题。你看,一旦进入主循环,就不必使用
标志
变量,相反,我建议你这样做:

def break_main_loop():  # how do I get this to execute?
  user_in = input()
  if user_in == 'stop': 
    return True # Returning integer 1 also works just fine
  else:
    return False # Returning integer 0 also works just fine


def stuff():
  pass


def main():
  # do stuff, e.g. getting other user input()
  while True:
    stuff()
    if break_main_loop(): # If the user inputs stop, the loop stops via the return statement automatically
      return

main()
如果您希望在不返回任何其他内容的情况下退出循环,并保持
main()
函数运行以完成任务:

def break_main_loop():  # how do I get this to execute?
  user_in = input()
  if user_in == 'stop': 
    return True # Returning integer 1 also works just fine
  else:
    return False # Returning integer 0 also works just fine


def stuff():
  pass


def main():
  # do stuff, e.g. getting other user input()
  while True:
    stuff()
    if break_main_loop(): 
      break
  #continue doing stuff

main()
现在,有一种更好的方法可以在不使用辅助函数的情况下中断循环
break\u main\u loop()
,如下所示:

def stuff():
  pass


def main():
  # do stuff, e.g. getting other user input()
  while True:
    stuff()
    if str(input("Do you wish to continue:[y/n]")) == 'n': 
      break
  #continue doing stuff

main()
这使您完全摆脱了helper函数


我希望这个答案是有帮助的D

为什么不使用键盘中断并捕获它来终止while循环?在LInux上,流行的方法是使用
Ctrl+C
停止控制台程序-您可以使用
KeyBoardInterrupt
捕获它。如果您不喜欢它,那么您必须找到
getchar()
getch()
来检查是否在不停止代码的情况下按下了键。若您创建GUI程序,那个么在不阻塞代码的情况下捕获密钥应该不会有问题。-它在不阻塞代码的情况下检查缓冲区中是否有密钥。我想我的问题应该用更好的措辞。我不想在每次循环后都要等待用户输入,我只是想在用户确实需要的情况下打破循环。在从stdin获取输入的过程中捕获SIGINT并不理想,但我现在看不到更正确的答案