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

每次你的时间check_solution和menu_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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值