計算函數調用次數的最佳方法是什麼?如果調用函數的次數是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()
哪些功能? – 2014-09-30 11:18:41
@PadraicCunningham我認爲這個函數是'def numCheck(userGuess)' – 2014-09-30 11:20:50
最簡單的方法是重構成for_in range(5):'loop - 看看https://docs.python.org /2/tutorial/controlflow.html#for-statements – jonrsharpe 2014-09-30 11:21:53