I was just wondering if I could remove some redundancy for a help function in a text-game. What I have right now is at the start of every function hint = 0, and hint is increased one every time an invalid answer is entered.
Here is what I have at the moment (inside every function):
hint = 0
valid = False
while valid == False:
print "Would you like to begin?"
begin = raw_input("> ")
if "yes" in begin:
valid = True
print "Great!\n"
start.start()
elif "no" in begin:
quit.quit()
else:
error.error(1)
hint += 1
if hint > 4:
print "\nYou may choose from \"yes\" and \"no\"."
The following code use decorator to separate hint logic and command logic. Every command is handled by a function which can be decorated by Hint object.
When the function return False, the count in Hint object will increase and when it’s larger than the limit, it will print out a hint message.
When the function return a tuple it will be called by the main loop.
Here is some test: