Search code examples
pythonfunctionaccumulate

How to use a function in another function in python


I have to do a code where I need to create a game that gets the user to guess an equation. After the end of each guess I have to show how much of the equation the user has guessed. For example if the equation is 1+2*3+4 and the user guessed that the equation has 3 the program would say your guess is correct and so far the equation you have guessed is ----3-- (the dashes represent how many characters the equation has. if the user guesses 2 next I need the equation that they have guessed so far to be --2-3-- but i can't get them to accumulate.

the functions that I am using are

def guessing1():
    '''
    the player is asked to make a guess and the result is printed
    '''
    wrongguesses=0
    if (guess1 in randomFormula):
         print "Your guess is correct!"
         wrongguesses=0
    else:
         print "Your guess is wrong!"
         wrongguesses +=1
         if (wrongguesses== max_guesses):
             print "Sorry, you've reached the maximum number of wrong guesses."
             print "Better luck next time!"
             playagain=raw_input("Do you want to play again? y-yes, n-no: ")
             if (playagain== n):
                print "The game is over."  

def formSoFar1():
   a=''
   for i in range (len(randomFormula)):
       if (randomFormula[i] == guess1):
           a += randomFormula[i]
       else:
          a+= "-"
   print "The formula you have guessed so far is: ",a

Any way that I change this code when I call the functions, I get either an error or it doesn't use the guesses from before. I'm not sure how to go about this.


Solution

  • I know this will not directly address your question, but I'll get back to that.

    I highly recommend writing out the logic you need to complete your goal. Do this on paper, and feel free to make arrows pointing between what's related, what criteria is needed to accomplish goals, and what the whole idea of the program actually is.

    Doing so will help shed some light on what your actual problem is that you want to solve, and how to go about doing it.

    So your first question is, what do you want to do?

    It appears you want to create a hangman like game, but using forumlas, rather than letters.

    The second thing, you want to keep track of the user's progress—both in achievement, as well as failure.

    Winning or losing is contingent on the second part, so this tells us something.

    There are several ways to go about doing this, but an easy way is to use a while loop.

    Why a while loop? It allows you to check conditions while performing a task multiple times.

    You want to check the conditions that the user has not won or lost. So something like this

    user_strikes = 0
    formula = "1+2*3+4"
    user_progress = "" # this will be updated
    while game_over is False and user_strikes < STRIKES_ALLOWED:
        # output "make a guess" or something
        if guess in answer:
            # update user_progress
            # perhaps check if user_progress is equal to the formula, then set game_over to True if it is.
        else:
            # update user_strikes. Check here if it's equal to STRIKES_ALLOWED and output a message, since the while loop will end after that iteration.
    # here you can check if game_over is True (user won), if strikes is equal to STRIKES_ALLOWED, and do logic based on that
    # there are quite a few ways to do this.
    

    Other things to keep in mind: Keep a tally of what numbers, symbols, etc the user has already guessed. If they guess 2 over and over again, maybe print out "Already guessed" or something like this. Rather than checking multiple variables dealing with game state, you can use just one variable that is set to a number. Check the value of that number, and base actions off that. So game_state = 1 #game won; game_state = 2 #game lost, etc.

    For your actual question, it seemed you were wondering about closures. Using closures may be beneficial for you, so please feel free to read up on them and decide if the situation calls for them.

    To finish, I highly recommend trying to figure out what sort of problem you have. From here, you can greatly save time and energy in figuring out the right way to solve it.