2014-09-30 370 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。