lundi 26 mars 2018

Python quiz program. Could you help me identify where I've gone wrong? Thanks

This is a small quiz program that loops 5 times, every time it loops a different totally random question is asked. I keep getting an error that says (rand_quiz() ) function not defined, although I have defined it. I think I've done it wrongly. Could you help me correct it. Code is below.

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()




Aucun commentaire:

Enregistrer un commentaire