函数和参数

时间:2018-11-26 09:27:47

标签: python python-3.x

我正在制作一个用户应该编写答案的游戏(question()函数)。在问题函数中,如果用户的答案错误或正确,我将使用变量1和0来获取信息。我还使用变量1和0来查看用户是否回答了问题。

def main():
    menu()
    anv_val = ber_val()
    val(anv_val)

def menu():
    print("1. game")
    print("2. stat")
    print("3. end")

def ber_val():
    val = input("Your choice: ")
    while val not in ["1", "2", "3"]:
        print("Print 1, 2 or 3.")
        val = input("Your choice: ")
    return val

def val(anv_val):
    if (anv_val == "1"):
        res = game()
        return res
    elif (anv_val == "2"):
        res = game()
        return stat(res)
    else:
        return end()

def question(quest, solu):
    print(quest)
    answer = input("Your answer: ")
    a_s = 1
    a_f = 0
    while (answer != solu):
        a_f = 1
        print("Try again")
        answer = input("Your answer: ")
    print("Correct!")
    a_f_s = [a_f, a_s]
    return a_f_s

def game():
    a_1 = question("Your name?", "Ricky")
    a_2 = question("Your name?", "Rong")
    a_3 = question("Your name?", "Bolly")
    sum_f = a_1[0] + a_2[0] + a_3[0]
    sum_s = a_1[1] + a_2[1] + a_3[1]
    sum_all = [sum_f, sum_s]
    return sum_all, main()

def stat(res):
    print("Questions you answered: " + str(res[1]))
    print("Wrong times: " + str(res[0]))
    return main()

main()

现在是我的问题。在函数game()中,我想返回名为a_f_s的变量,并在函数stat()中使用该信息(该信息是错误和已回答问题的数量)。但是问题是在函数val(anv_val)中-如果用户打印“ 2”,则函数game()将运行,但是我希望stat()运行。我有点困惑。

感谢所有帮助!

2 个答案:

答案 0 :(得分:2)

我添加了一个全局变量end_game,以控制main()中的while循环。这样,您将无需继续调用game(),从而简化了程序逻辑。请注意,game()现在不会返回对main()的呼叫。

我还使res为全局变量,仅在调用game()时才对其进行修改。在stat()中,如果全局res不是None,则用于打印统计信息。

end_game = False
res = None

def main():
    while not end_game:
        menu()
        anv_val = ber_val()
        val(anv_val)

def menu():
    print("1. game")
    print("2. stat")
    print("3. end")

def ber_val():
    val = input("Your choice: ")
    while val not in ["1", "2", "3"]:
        print("Print 1, 2 or 3.")
        val = input("Your choice: ")
    return val

def val(anv_val):
    global end_game, res
    if (anv_val == "1"):
        res = game()
    elif (anv_val == "2"):
        stat()
    else:
        end_game = True


def question(quest, solu):
    print(quest)
    answer = input("Your answer: ")
    a_s = 1
    a_f = 0
    while (answer != solu):
        a_f = 1
        print("Try again")
        answer = input("Your answer: ")
    print("Correct!")
    a_f_s = [a_f, a_s]
    return a_f_s

def game():
    a_1 = question("Your name?", "Ricky")
    a_2 = question("Your name?", "Rong")
    a_3 = question("Your name?", "Bolly")
    sum_f = a_1[0] + a_2[0] + a_3[0]
    sum_s = a_1[1] + a_2[1] + a_3[1]
    sum_all = [sum_f, sum_s]
    return sum_all

def stat():
    if res is not None:
        print("Questions you answered: " + str(res[1]))
        print("Wrong times: " + str(res[0]))
    return

main()

作为奖励,这是我编写代码的一种简单方法:

def main():
    res = None

    while True:
        print_menu()
        choice = get_choice()

        if (choice == "1"):
            res = game()
        elif (choice == "2"):
            stat()
        else:
            return

def print_menu():
    print("1. game")
    print("2. stat")
    print("3. end")

def get_choice():
    val = input("Your choice: ")
    while val not in ["1", "2", "3"]:
        print("Print 1, 2 or 3.")
        val = input("Your choice: ")
    return val

def question(quest, solu):
    print(quest)
    answer = input("Your answer: ")
    a_s = 1
    a_f = 0
    while (answer != solu):
        a_f = 1
        print("Try again")
        answer = input("Your answer: ")
    print("Correct!")
    a_f_s = [a_f, a_s]
    return a_f_s

def game():
    a_1 = question("Your name?", "Ricky")
    a_2 = question("Your name?", "Rong")
    a_3 = question("Your name?", "Bolly")
    sum_f = a_1[0] + a_2[0] + a_3[0]
    sum_s = a_1[1] + a_2[1] + a_3[1]
    sum_all = [sum_f, sum_s]
    return sum_all

def stat():
    if res is not None:
        print("Questions you answered: " + str(res[1]))
        print("Wrong times: " + str(res[0]))
    return

main()

答案 1 :(得分:1)

两个观察结果:

  1. %localappdata%\Microsoft\VisualStudio\<version>将始终为所有用户打印相同的结果,因为在用户回答所有问题之前该程序不会继续进行。
  2. 您应该使用Questions you answered:来增加错误答案的数量。