我如何称呼我的;计数();从“内部”执行的功能;问题()”;python中的函数?

我如何称呼我的;计数();从“内部”执行的功能;问题()”;python中的函数?,python,python-3.x,Python,Python 3.x,如何从question()函数中调用我的“counting()”函数?所以我只需要指定它应该运行一次——不管我在我的问答游戏中有多少个问题。 我试过了,但什么也没用 请帮帮我,谢谢 p、 我的问题是关于瑞典语的,但没关系 from time import sleep def main(): option() def start_menu():

如何从question()函数中调用我的“counting()”函数?所以我只需要指定它应该运行一次——不管我在我的问答游戏中有多少个问题。 我试过了,但什么也没用

请帮帮我,谢谢

p、 我的问题是关于瑞典语的,但没关系

    from time import sleep
        
        def main():
            option()
            
        def start_menu():
            
            """This will display the main menu"""
            print("*"*40)
            print("MAIN MENU")
            print("*"*40)
            print("1. Start the quiz") 
            print("2. Statistics")
            print("3. Exit game")
        
        def option():
            """This should get user input """
            while True:
                start_menu()
                option= input("Write your choice here [1-3]")
                if option=="1":
                    qustion_game()
                elif option=="2": 
                    statistics()
                elif option=="3": 
                    game_over()
                    return
                else:
                    print("he selection you specified was not valid, [1-3]")
        
        def qustion_game():
            """Frågesporten"""
            print("♡"*40)
            print("Welcome to this quiz ")
            print("♡"*40)
            print("")
        
            sleep(1)
            question("Annika hade 4 barn, den första hette januari, den andra hette februari, tredje hette april, vad hette den fjärde.?", "vad")
            print("")
            counting()

           
        
            sleep(1)
            print("♡"*40)
            question("Vem får lön utan att jobba en enda dag i hela sitt liv?", "nattvakt" )
            print("")
            counting()
           
        
            sleep(1)
            print("♡"*40)
            question("Lägg till 1000 till 40. Lägg till 1000. Lägg till 30. Lägg till 1000 igen. Lägg nu till 20. Lägg till 1000 igen.Lägg nu till 10.Vad är svaret?", "4100")
            print("")
            counting()
           
          
        
        
        def question(quiz,quiz_answer):
            """Outputen av frågor"""
            user_guess=input(quiz)
            while user_guess != quiz_answer:
                print("Sorry, try again...")
                fail_answer()
                user_guess=input(quiz)
            print("")
            print("*"*40)
            print("Correct")
         
            
        
        def statistics():
            """Provides the user with statistics on how many questions they have answered and how many errors they have made """
            print("Statistics")
            print("*"*40)
            
            print("You have totally answered " + str(answered) +" questions") 
            print("Off " +str(answered)+ " answer, have you answered incorrectly on " + str(fail))
        
        
        def fail_answer():
            """prints how many questions the user has answered"""
            global answered
            answered = answered + 1
        
        def counting():
            """prints how many errors the user has made"""
            global fail
            fail = fail + 1
           
        def game_over():
            """Exit the game"""
            print ("Game over, see you next time")
        
        fail = 0
        answered = 0
        main()
        
    
         

我认为解决这个问题的最好方法是使用一个类,它可以保持其内部状态。然后,您可以根据需要多次调用该函数,它将保持变量的内部状态。然后,您可以调用class.failed_answer(),次数与用户提问失败的次数相同,并且它将始终从该点继续,避免重置变量,无论您有多少问题

完整代码(正如作者在评论中提出的问题)如下:

from time import sleep

class Statistics:
    def __init__(self):
        self.answered = 0
        self.failed = 0

    def counting(self):
        self.answered += 1

    def failed_answer(self):
        self.failed += 1

def main():
    option()

def start_menu():
    """This will display the main menu"""
    print("*"*40)
    print("MAIN MENU")
    print("*"*40)
    print("1. Start the quiz")
    print("2. Statistics")
    print("3. Exit game")

def option():
    """This should get user input """
    stats = Statistics()
    while True:
        start_menu()
        option= input("Write your choice here [1-3]")
        if option=="1":
            qustion_game(stats)
        elif option=="2":
            statistics(stats)
        elif option=="3":
            game_over()
            return
        else:
            print("The selection you specified was not valid, [1-3]")

def qustion_game(stats):
    """Frågesporten"""
    print("♡"*40)
    print("Welcome to this quiz ")
    print("♡"*40)
    print("")

    sleep(1)
    question("Annika hade 4 barn, den första hette januari, den andra hette februari, tredje hette april, vad hette den fjärde.?", "vad", stats)
    print("")



    sleep(1)
    print("♡"*40)
    question("Vem får lön utan att jobba en enda dag i hela sitt liv?", "nattvakt", stats)
    print("")


    sleep(1)
    print("♡"*40)
    question("Lägg till 1000 till 40. Lägg till 1000. Lägg till 30. Lägg till 1000 igen. Lägg nu till 20. Lägg till 1000 igen.Lägg nu till 10.Vad är svaret?", "4100", stats)
    print("")

def question(quiz, quiz_answer, stats):
    """Outputen av frågor"""
    user_guess=input(quiz)
    stats.counting()
    while user_guess != quiz_answer:
        print("Sorry, try again...")
        stats.failed_answer()
        user_guess=input(quiz)
    print("")
    print("*"*40)
    print("Correct")

def statistics(stats):
    """Provides the user with statistics on how many questions they have answered and how many errors they have made """
    print("Statistics")
    print("*"*40)

    print("You have totally answered %d questions" %stats.answered)
    print("Off %d answers have you answered incorrectly on %d" %(stats.answered, stats.failed))

def game_over():
    """Exit the game"""
    print ("Game over, see you next time")

if __name__ == '__main__':
    main()

您需要取消对整个代码的插入

此外,您的函数
fail\u answer
counting
似乎做了错误的事情-它们需要交换

def fail_answer():
    """prints how many errors the user has made"""
    global fail
    fail = fail + 1

def counting():
    """prints how many questions the user has answered"""
    global answered
    answered = answered + 1
除此之外,如果您在打印
Correct
后从内部插入对
counting
的调用,则您的代码以其现有形式工作:

def question(quiz,quiz_answer):
    ... existing code goes here ...
    print("Correct")
    counting()

为什么
def
前面有一个选项卡?可能是SO编辑器没有有效使用@Andy_Yeu如果我植入这个,我可以删除游戏中问题下的计数()?谢谢,是的。您可以在question()函数中添加此函数,因为每次调用question()时,它也会调用counting()函数。然后,您可以在statistics()函数中最后调用它一次,并将其值减1,它将为您提供回答问题的正确值。我尝试将其添加到代码中,但没有成功,我认为我添加了错误的值。我想知道你们是否可以用yield关键字编辑我的代码?用完整的代码编辑我的答案。如果它解决了你的问题,请勾选它作为正确答案:)谢谢你,我真的很感谢你帮助我。我再次尝试了该代码,但我想知道,如果name='main:main(),为什么会隐含此代码?代码不起作用