[Tutor] Coding for a Secret Message in a Game

Danny Yoo dyoo at hashcollision.org
Fri Dec 13 20:19:07 CET 2013


As you've just started Python, you may not know about functions yet,
but the question you're asking sounds very much like one that a
function will help with.  You can design functions that do a specific
job: in this case, it sounds like you're asking for a function that
takes the number of failures that have taken place so far, and returns
the right message.

For example:

#######################################
def GetFailureMessage(failure_count):
    """Returns a message given how many times we've seen failure."
    if failure_count <= 1:
        return "Try again"
    else:
        return "Please try again"

## Let's try it out:
print(GetFailureMessage(0))
print(GetFailureMessage(1))
print(GetFailureMessage(2))
#######################################

Given that, then your while loop just needs to be able to count how
many times you've gone through so far, and provide that count to this
GetFailureFunction().


The nice thing about functions is that you can develop them
independently of the rest of your program, so your program doesn't
have to be all one big thing.  Look into them: I think they're one of
the core pieces of learning how to program in general.


More information about the Tutor mailing list