在Python 3.4中重新启动函数
我的python作业需要帮助。我们必须制作一个测验程序,我在重新启动函数时遇到了问题 我需要像continue这样的东西,但需要再次运行该函数。另外,关于从函数返回值的一些提示也没什么坏处!谢谢~另外,我在两周前才开始使用python,所以这对我来说是非常先进的。编辑:感谢用户:2ps!:D在Python 3.4中重新启动函数,python,function,return,python-3.4,Python,Function,Return,Python 3.4,我的python作业需要帮助。我们必须制作一个测验程序,我在重新启动函数时遇到了问题 我需要像continue这样的东西,但需要再次运行该函数。另外,关于从函数返回值的一些提示也没什么坏处!谢谢~另外,我在两周前才开始使用python,所以这对我来说是非常先进的。编辑:感谢用户:2ps!:D #Quiz YAY! # #Name Removed # #Version 1.0 # score = 0; modPassword = "200605015" def modMode(score):
#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
函数的返回,只需确保在末尾返回一些内容即可:
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
此操作将一直运行,直到用户在exit中键入。请缩进属性并使用正确的语法。此代码当前无效,无法运行。缩进无效。请修理。此外--分号在Python中不是语句终止符。您需要学习语法的基本知识。
虽然为True:modScore()
您也不必通过score,因为它已经是全局的了。@TylereBastian正如我所说,我对python非常陌生,需要更深入的解释。当然,除非它不会打扰你……好吧,就像我说的,我是新来的,无论你做了什么,都有效!什么是例外?如果你不介意的话…它被用于很多事情。在这个特殊的例子中,我们只是将其作为函数的一种聪明的方式,让调用方知道用户已经说过他或她不想继续循环,并立即停止循环。这里有详细的解释(错误处理最常用的例外情况):进入Overide模式打开Overide控制台Enter命令:manScoreChng您希望分数是多少?32 0我只是复制并粘贴了我的代码并运行了它,它的输出与您得到的不一样。你能重新发布整个程序吗?好的,我做了一些事情,得到了一个无输出,现在理解了异常