Python 3:通过多个函数返回变量

时间:2019-04-09 22:56:50

标签: python function loops return

已给我一个基本的python问题,要求我进行一个简单的加法测验。但是,我似乎无法返回我的count变量,该变量应该更新用户已回答的正确问题的数量,从而使它停留在0。我尝试在每个包含该变量的函数中将变量count定义为参数,但仍然可以不行。假设用户要回答4个问题并获得3个正确答案,它将显示为“您已回答4个问题且3个正确答案”,而是显示“您已经回答4个问题且0个正确答案”。

5 个答案:

答案 0 :(得分:1)

正如评论所述,每次调用check_solution或menu_option时,您都将count初始化为0。

您似乎想使用count = count传递给函数的变量。

快速编辑:

您实际上不需要返回计数。在Python中,变量是通过引用传递的,因此只要传递给函数,您的计数就会被更新。

答案 1 :(得分:1)

您需要获取check_solution(user_answer, randsum, count)的回报并返回该计数

答案 2 :(得分:1)

每次调用check_solutionmenu_option函数时,都会初始化count = 0。这意味着每次用户提出另一个问题时,count都会两次重置为0。您将要删除这些count = 0调用,并且还希望捕获更新以计入menu_option中。您的最终程序应如下所示:

import random

def get_user_input():
    count = 0
    user_input = int(input("Enter 1 to play or press 5 to exit: "))
    while user_input > 5 or user_input <= 0:
        user_input = int(input("Invalid menu option. Try again: "))
        menu_option(user_input, count)

        if user_input == "5":
            print("Exit!")

    return user_input

def get_user_solution(problem):
    answer = int(input(problem))
    return answer

def check_solution(user_solution, solution, count):
    curr_count = count
    if user_solution == solution:
        curr_count += 1
        print("Correct.")

    else:
        print("Incorrect.")
    print(curr_count)
    return curr_count

def menu_option(index, count):
    if index == 1:
        num1 = random.randrange(1, 21)
        num2 = random.randrange(1, 21)
        randsum = num1 + num2
        problem = str(num1) + " " + "+" + " " + str(num2) + " " + "=" + " "
        user_answer = get_user_solution(problem)
        count = check_solution(user_answer, randsum, count) # count returned by check_solution is now being captured by count, which will update your count variable to the correct value

    return count

def display_result(total, correct):
    if total == 0:
        print("You answered 0 questions with 0 correct.")
        print("Your score is 0%. Thank you.")
    else:
        score = round((correct / total) * 100, 2)
        print("You answered", total, "questions with", correct, "correct.")
        print("Your score is", str(score) + "%.")

def main():
    option = get_user_input()
    total = 0
    correct = 0
    while option != 5:
        total = total + 1
        correct = menu_option(option, correct)
        option = get_user_input()

    print("Exiting.")
    display_result(total, correct)

main()

答案 3 :(得分:0)

这是几个逻辑错误的总结。

  • 您将def menu_option(index, count=0):用作输入,然后立即覆盖它。

    • 我会说count=0。如果未提供任何变量(创建默认值),则会设置count,否则将check_solution()设置为传递给函数的任何内容
  • 您的check_solution(user_answer, randsum, count)函数返回一个数字,但是当您使用output调用它时,您永远不会将此返回值分配给任何东西/再次使用它。

    • 您可以将其分配给变量(例如return output),然后分配给return count而不是import random def get_user_input(count = 0): user_input = int(input("Enter 1 to play or press 5 to exit: ")) while user_input > 5 or user_input <= 0: user_input = int(input("Invalid menu option. Try again: ")) menu_option(user_input, count) if user_input == "5": print("Exit!") return user_input def get_user_solution(problem): answer = int(input(problem)) return answer def check_solution(user_solution, solution, count): count = 0 if user_solution == solution: count += 1 print("Correct.") else: print("Incorrect.") return count def menu_option(index, count=0): if index == 1: num1 = random.randrange(1, 21) num2 = random.randrange(1, 21) randsum = num1 + num2 problem = str(num1) + " " + "+" + " " + str(num2) + " " + "=" + " " user_answer = get_user_solution(problem) output = check_solution(user_answer, randsum, count) return output def display_result(total, correct): if total == 0: print("You answered 0 questions with 0 correct.") print("Your score is 0%. Thank you.") else: score = round((correct / total) * 100, 2) print("You answered", total, "questions with", correct, "correct.") print("Your score is", str(score) + "%.") def main(): option = get_user_input() total = 0 correct = 0 while option != 5: total += 1 correct = menu_option(option, correct) option = get_user_input() print("Exiting.") display_result(total, correct) main()

修复这些问题仍不能完全解决问题,但可以解决一些问题(现在它卡在了“您用1个正确答案回答了x个问题”上):

import random

def generate_question():
    num1 = random.randint(1, 25)
    num2 = random.randint(1, 25)
    question = '{} + {} = '.format(num1, num2)
    answer = num1 + num2
    return question, answer

def main():
    correct = 0
    total = 0
    option = True
    while option != '5':
        total += 1
        question, answer = generate_question()
        user_guess = int(input(question))
        if user_guess == answer:
            print('Correct.')
            correct += 1
        else:
            print('Incorrect.')
        option = input("Enter 5 to exit, or anything else to play again")
    print('You answered {} questions with {} correct'.format(total, correct))

main()

我认为更简单的方法看起来像:

{{1}}

答案 4 :(得分:0)

您可以选择在所有功能之前将count初始化为0,从而创建一个全局变量。然后,您将不需要在任何函数上声明它或将其作为参数传递。