计算函数调用次数的最佳方法是什么?如果调用函数的次数是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