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

f0ofjuux  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(146)

这是我的代码副本:)这个游戏是我个人活动的一部分。所以在购买线索**(从:def game_hints)我想返回到def Game_process。**

import random     
    SCORE = 0
    ROUNDS = 1
          
    def player_stats():
        print(f"SCORE: {SCORE} | ROUNDS: {ROUNDS}")
    
    def game_hints():
        print("Would you like purchase a hint for 5 points? [1/2]: ")
        USER_HINT = int(input())
        global SCORE

        if USER_HINT == 1:
           points -= 5
        if Mystery_NUM % 2 == 0:
           divisibility = "even"
        else:
           divisibility = "odd"    
        if player_guess > Mystery_NUM:
           value = "smaller"
        else:
           value = "larger"  
        
        print(f"CLUE: Mystery Number is {divisibility} and try a {value} guess | -5 POINTS")

 
      
    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():
        print("Would you like purchase a hint for 5 points? [1/2]: ")
        USER_HINT = int(input())
        global SCORE

        if USER_HINT == 1:
           points -= 5
        if Mystery_NUM % 2 == 0:
           divisibility = "even"
        else:
           divisibility = "odd"    
        if player_guess > Mystery_NUM:
           value = "smaller"
        else:
           value = "larger"  
        
        print(f"CLUE: Mystery Number is {divisibility} and try a {value} guess | -5 POINTS")
aelbi1ox

aelbi1ox1#

首先,必须删除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()

相关问题