Home > OS >  Python - How to return to previous function after calling another function?
Python - How to return to previous function after calling another function?

Time:10-30

Heres the copy of my code :) this game is part of my personal activity. so after purchasing a clue (from: def game_hints) i want to return to def Game_process.

import random     
    SCORE = 0
    ROUNDS = 1
          
    def player_stats():
        print(f"SCORE: {SCORE} | ROUNDS: {ROUNDS}")
    
    def game_hints(USER_GUESS, Mystery_NUM):
        print("Would you like purchase a hint for 5 points? [1/2]: ")
        USER_HINT = int(input())
        global SCORE
        if USER_HINT == 1:
         SCORE= SCORE - 5
         if USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 0:
          print("Mystery Num is even and try a smaller guess")
         elif USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 1:
          print("Mystery Num is odd and try a smaller guess")
         elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 0: 
          print("Secret Num is even and try a larger guess")
         elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 1:
          print("Mystery Num is odd and try a larger guess") 
 
      
    def Game_Process():
     global ROUNDS
    
     while True:
      if ROUNDS <= 10:   
        Mystery_NUM = random.randrange(10)
        print(Mystery_NUM) #remove before final product 
        print("Guess the num [1-10]: ")
        USER_GUESS = int(input())
        if USER_GUESS == Mystery_NUM:
            print("\nGood Job!  5 Coins!")
            global SCORE
            SCORE = SCORE   10
            ROUNDS  = 1
            player_stats()    
        else:
            print("Wrong! Try Again")
            game_hints(USER_GUESS, Mystery_NUM)
      else:
        print("Game Over!")
        Game()        
    
    def Game():
        user_opt = input("\"Welcome to Guess Game\" \nPress [Y] to Play or [N] to Exit: ").lower()
        if user_opt == "n":
            print("Good bye!")
            exit()
        elif user_opt == "y":
            Game_Process()
        else:
            print("Invalid Input! [1/2]")
            Game()
    Game()

As shown below, this is the fuction for the hints. I was able to call this function but the only problem is that the after this fuction is done, it changes the Myster_Num.

def game_hints(USER_GUESS, Mystery_NUM):
    print("Would you like purchase a hint for 5 points? [1/2]: ")
    USER_HINT = int(input())
    global SCORE
    if USER_HINT == 1:
     SCORE= SCORE - 5
     if USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 0:
      print("Secret Num is even and try a smaller guess")
     elif USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 1:
      print("Secret Num is odd and try a smaller guess")
     elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 0: 
      print("Secret Num is even and try a larger guess")
     elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 1:
      print("Mystery Num is odd and try a larger guess") 

CodePudding user response:

First, you must remove the else statement in the game_hints function because it restarts a full GameProcess and therefore indeed recompute a mysterious number.

Then, when you exit game_hints and come back to GameProcess, you must not come back to the big loop because it will indeed recompute a mysterious number. The solution is to have an inner loop inside each round that you exit only if the player guessed the correct value using the break keyword.

def Game_Process():
    SCORE = 0
    ROUNDS = 1
    while True:
        if ROUNDS <= 10:
            Mystery_NUM = random.randrange(10)
            print(Mystery_NUM)  # remove before final product
            while True:
                print("Guess the num [1-10]: ")
                USER_GUESS = int(input())
                if USER_GUESS == Mystery_NUM:
                    print("\nGood Job!  5 Coins!")
                    SCORE = SCORE   10
                    ROUNDS  = 1
                    player_stats()
                    break
                else:
                    print("Wrong! Try Again")
                    game_hints(USER_GUESS, Mystery_NUM)
        else:
            print("Game Over!")
            Game()
  • Related