Python测验程序。你能帮我辨认出错的地方吗?谢谢

时间:2018-03-26 18:12:31

标签: python python-3.x function random

这是一个小的测验程序,循环5次,每次循环一个不同的完全随机的问题。我一直得到一个错误,说明(rand_quiz())函数没有定义,虽然我已经定义了它。我想我做错了。你能帮我纠正一下吗?代码如下。

import random
import operator

operators = {
            "+":operator.add,
            "-":operator.sub,
            "*":operator.mul,
            "/":operator.truediv,
            }

questions = 0
star = 0


def star_score():

    while questions <= 4:
        star = star + 1
        return star


    def check_ans():

        if que_ans == que:
            print("Correct! You earned yourself a star.")
            star_score()

        elif que_ans != que:
            print("Wrong! Better luck next time.")


        def rand_quiz():
            rand_num1 = random.randint(9, 999)
            rand_num2 = random.randint(9, 999)

            ops = random.choice(list(operators.keys()))

            que = int(operators[ops](rand_num1, rand_num2))

            print("What is",rand_num1, ops, rand_num2)
            que_ans = input(">>>")

            if que_ans.isdigit():
                check_ans()

            else:
                return("Error! Invalid input.")    


def quiz_loop():

    while questions <= 4:
        rand_quiz()


quiz_loop()

1 个答案:

答案 0 :(得分:0)

在python中,缩进非常重要。您已在缩进级别定义了一些函数。您必须在基础级别定义函数。