2014-09-30 177 views
-1

计算函数调用次数的最佳方法是什么?如果调用函数的次数是5次,程序将停止?计数函数调用python

from random import randint 
from functools import wraps 
randNum = randint(1,100) 



userGuess = int(input('Please guess a number between 0 and 100: ')) 
yesNo = 'y' 
while yesNo == 'y': 
    while randNum != userGuess: 
     def numCheck(userGuess): 
      if userGuess == randNum: 
       return('Well done!') 
      elif userGuess > randNum: 
       return('Too high!') 
      else: 
       return('Too low!') 

     def tryAgain(numCheck): 
      if numCheck == 'Well done!': 
       return(numCheck(userGuess)) 
      else: 
       return('Try again') 

     print(numCheck(userGuess)) 
     print(tryAgain(numCheck)) 

     userGuess = int(input('Please guess a number between 0 and 100: ')) 

    yesNo = str(input('Continue? Y/N: ')).lower() 
+1

哪些功能? – 2014-09-30 11:18:41

+0

@PadraicCunningham我认为这个函数是'def numCheck(userGuess)' – 2014-09-30 11:20:50

+0

最简单的方法是重构成for_in range(5):'loop - 看看https://docs.python.org /2/tutorial/controlflow.html#for-statements – jonrsharpe 2014-09-30 11:21:53

回答

0

作为维护全局(eww)字典的装饰器怎么样?每个函数都是一个关键字(完全限定名称),该值是该函数的最大调用次数。每次执行封装函数时,它都会检查值!= 0,执行函数并减少计数。

如果您只需要一个函数,那么不太通用的装饰器就可以使用全局变量而不是字典,并且检查执行 - 如上所述递减该值。

+0

虽然在这个问题的特定情况下,管理循环内部更明智。 – aneroid 2014-09-30 11:34:02

1

你应该避免你的内循环功能,只是循环,直到用户已经有五guesses或者如果他们猜测正确的:

def main(): 
    randNum = randint(1,100) 
    count = 0 
    while count < 5: 
     userGuess = int(input('Please guess a number between 0 and 100: ')) 
     if userGuess == randNum: 
      print('Well done!') 
      break 
     elif userGuess > randNum: 
      print('Too high!') 
     else: 
      print('Too low!') 
     count += 1 
    yesNo = input('Continue? Y/N: ').lower() # ask user to play again 
    if yesNo == "y": 
     main() # restart the function if the user enters y 
    else: 
     return "Game Over" 

或者只是使用range在数量范围在环打破允许猜测:

for guess in range(5): 
      userGuess = int(input('Please guess a number between 0 and 100: ')) 
      if userGuess == randNum: 
       print('Well done!') 
       break 
      elif userGuess > randNum: 
       print('Too high!') 
      else: 
       print('Too low!') 
    yesNo = input('Continue? Y/N: ').lower() 
    if yesNo == "y": 
     main() 
    else: 
     return "Game Over" 
+0

+1该函数的工作是确定数字是否太高,太低或恰到好处,而不是*来管理其调用者的控制流。 – chepner 2014-09-30 11:53:15

1

不太一个完整的答案,但你可能想看看sys.settrace如果你想监控的功能。这可能会更先进一点

>>> import sys 
# start by defining a method which we will track later 
>>> def blah(): 
... print('blah') 
... 
# we make a set of functions, such to avoid the "under the hood" functions 
>>> functions = set(['blah']) 
# define what we want to do on the function call 
>>> def tracefunc(frame, event, args): 
     # if the event is a function being called, and the function name is on our set of functions 
... if event == 'call' and frame.f_code.co_name in functions: 
...  print('function called! <function {}>'.format(frame.f_code.co_name)) 
... 
# set our trace to the function we described above 
>>> sys.settrace(tracefunc) 
>>> blah() 
function called! <function blah> 
blah 
0

我认为你可以数到一定数量,然后引发一个例外Catch。