Python基本菜单,创建了一个“循环”。这不好吗?

时间:2014-07-28 13:01:02

标签: python loops menu

我在python中写了一个基本的用户界面,但我有一个问题。这是代码:

def mainUI():
  options=["doSomething","exit"]
  index=0
  print options[index]
  command=raw_input("Give a command: ")
  while True:
    if command="select":
      print "you selected ",options[index]
    elif command="up":
      index=UIListIndexIncrease(index,len(options))
      print options[index]
      command=raw_input("Give a command: ")
    elif command="down":
      index=UIListIndexDecrease(index)
      print options[index]
      command=raw_input("Give a command: ")
    time.sleep(0.25)

def mainDispatch(index):
  if index==0:
    yesOrNoMenu(aFunctionThatDoesSomething)
  elif index==1:
    exitProgram()


def aFunctionThatDoesSomething():
  ...
  mainUI()


def yesOrNoMenu(function,*opargs):   #<------ this function never exits
  index=0
  options=["are you sure? \n [no] yes","are you sure? \n no [yes]"]
  print options[index]
  x=raw_input("give a command: ")
  while True:
    if x=="select":
      dispatchYesNo(index,function,*opargs)  #<--------- the function is passed to the dispatch function and is executed there, the function returns to MainUI().
                                             #Then it goes from the dispatch back to the main function
      break    # <--------------------
    if x=="up":
      index=UIListIndexDecrease(index)    
      print options[index]
      x=raw_input("give a command: ")
    if x=="down":
      index=UIListIndexIncrease(index,len(options))
      print options[index]
      x=raw_input("give a command: ")
    time.sleep(0.25)
  # all the code here is executed at the end, when the script ends


def dispatchYesNo(index,function,*opargs):
  if index == 0:
     mainUI()
  elif index == 1:
     function(*opargs)

通过从函数aFunctionThatDoesSomething()返回指向mainUI(),我们创建了一个“loop”。我们从mainUI()开始,我们做了一些事情,现在我们回到了mainUI()。 我不确定这是否是一个问题,但是通过这样做,yesOrNoMenu永远不会退出,我们会在第一个yesOrNoMenu调用中继续“嵌套功能调用”。 通过这样做,堆栈跟踪可以变得非常大。

File "./transfer.py", line 383, in <module>
    main()
  File "./transfer.py", line 369, in main
    mainUI()
  File "./transfer.py", line 240, in mainUI
    mainDispatch(index)
  File "./transfer.py", line 227, in mainDispatch
    aFunctionThatDoesSomething()
  File "./transfer.py", line 286, in aFunctionThatDoesSomething
    yesOrNoMenu(aFunctionThatDoesSomething)
  File "./transfer.py", line 310, in yesOrNoMenu
    dispatchYesNo(index,function,*opargs)
  File "./transfer.py", line 324, in dispatchYesNo
    mainUI()
  File "./transfer.py", line 240, in mainUI
    mainDispatch(index)
  File "./transfer.py", line 227, in mainDispatch
    aFunctionThatDoesSomething()
  File "./transfer.py", line 286, in aFunctionThatDoesSomething
    yesOrNoMenu(aFunctionThatDoesSomething)
  File "./transfer.py", line 310, in yesOrNoMenu
    dispatchYesNo(index,function,*opargs)
  File "./transfer.py", line 324, in dispatchYesNo
    mainUI()
  File "./transfer.py", line 240, in mainUI
    mainDispatch(index)
  File "./transfer.py", line 227, in mainDispatch
    aFunctionThatDoesSomething()
  File "./transfer.py", line 286, in aFunctionThatDoesSomething
    yesOrNoMenu(aFunctionThatDoesSomething)
  File "./transfer.py", line 310, in yesOrNoMenu
    dispatchYesNo(index,function,*opargs)
  File "./transfer.py", line 324, in dispatchYesNo
    mainUI()
  File "./transfer.py", line 240, in mainUI

我不知道这是不好的代码还是这是正常的。谁能帮我这个? 感谢

1 个答案:

答案 0 :(得分:1)

是的,这可能是不好的做法。 Python有一个最大递归限制;一旦你的堆栈跟踪有一千个函数,你的程序就会崩溃RuntimeError

我建议修改aFunctionThatDoesSomething,以便它不会调用mainUI。 main函数有一个while True:循环,所以它应该在aFunction完成后自然重复,即使你没有明确地调用它。