在 Python 3.4 中重新启动一个函数

Restarting a function in Python 3.4

我的 python 作业需要帮助。我们必须制作一个测验程序,但我无法重新启动功能。

我需要类似 continue 的东西,而是再次运行该函数。此外,一些关于从函数返回值的技巧也无妨!谢谢! ~此外,我 2 周前才开始使用 python,所以这对我来说非常先进。编辑:感谢用户:2ps! :D

#Quiz YAY!
#
#Name Removed
#
#Version 1.0
#
score = 0;
modPassword = "200605015"
def modMode(score):
   print("Entering Overide Mode");
   print("Opening Overide Console")
   cmd = input("Enter Command call exit{} to exit:  ")
   if cmd == "corr":
      print("Adding one point")
      score=score+1
      return(score);
   elif cmd== "manScoreChng":
      score=int(input("What do want the score to be?"));
   elif cmd == 'stop{}':
      raise Exception('Quit by User')

score = modMode(score);
print(score);

要捕获 modMode 函数的 return,只需确保 return 末尾有内容:

score = 0;
modPassword = "200605015"
def modMode(score):
   print("Entering Overide Mode")
   print("Opening Overide Console")
   cmd = input("Enter Command:  ")
   if cmd == "corr":
      print("Adding one point")
      score = score+1
   elif cmd == "manScoreChng":
      score = int(input("What do want the score to be?"))
   elif cmd == 'exit':
      raise Exception('Bye!')
   return int(score) # MAKE SURE YOU HAVE THIS LINE HERE

要一遍又一遍地调用 modScore 命令,请使用循环。

try:
  while True:
    score = modMode(score) # grab the returned value from modMode by using `=`
    print(score)
except Exception:
  pass

这将 运行 直到用户键入退出。