Python - 如何在调用另一个函数后return到上一个函数?

Python - How to return to previous function after calling another function?

这是我的代码的副本:) 这个游戏是我个人的一部分 activity。 所以在购买线索后 (from: def game_hints) 我想 return 到 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()

如下图,这是提示的功能。我能够调用这个函数,但唯一的问题是这个函数完成后,它改变了 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") 

首先,您必须删除 game_hints 函数中的 else 语句,因为它会重新启动一个完整的 GameProcess,因此确实重新计算一个神秘的数字。

然后,当你退出game_hints,回到GameProcess,一定不要再回到大循环因为它确实会重新计算一个神秘的数字。解决方案是在每一轮中都有一个内部循环,只有当玩家使用 break 关键字猜对了正确值时才退出。

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()