0
import random

print("Welcome To Arthur's Quiz,\nfeel free to revise the answers once you have completed.\n")

redos=0
def multiplyquestion():
    first_num=random.randint(1,100)
    second_num=random.randint(1,100)
    real_answer= first_num*second_num
    human_answer=0

    while real_answer!=human_answer:

        human_answer = float(input("What is "+str(first_num)+" x "+str(second_num)+" : "))

        if real_answer==human_answer:
            print("Well done!, The answer was: "+str(real_answer)+"\n")

        else:
            print("Incorrect answer, Please Try Again.\n")
            global redos
            redos = redos+1
def divisionquestion():
    first_num=random.randint(1,100)
    second_num=random.randint(1,100)
    real_answer= first_num/second_num
    human_answer=0
    while real_answer!=human_answer:

        human_answer = float(input("What is "+str(first_num)+" / "+str(second_num)+" : "))

        if real_answer==human_answer:
            print("Well done!, The answer was: "+str(real_answer)+"\n")

        else:
            print("Inccorrect answer, Please Try Again.\n")
            global redos
            redos = redos+1
def additionquestion():
    first_num=random.randint(1,100)
    second_num=random.randint(1,100)
    real_answer= first_num+second_num
    human_answer=0
    while real_answer!=human_answer:

        human_answer = float(input("What is "+str(first_num)+" + "+str(second_num)+" : "))

        if real_answer==human_answer:
            print("Well done!, The answer was: "+str(real_answer)+"\n")

        else:
            print("Inccorrect answer, Please Try Again.\n")
            global redos
            redos = redos+1       
def subtractquestion():
    first_num=random.randint(1,100)
    second_num=random.randint(1,100)
    real_answer= first_num-second_num
    human_answer=0
    while real_answer!=human_answer:

        human_answer = float(input("What is "+str(first_num)+" - "+str(second_num)+" : "))

        if real_answer==human_answer:
            print("Well done!, The answer was: "+str(real_answer)+"\n")

        else:
            print("Inccorrect answer, Please Try Again.\n")
            global redos
            redos = redos+1
def main():
    for i in range(0,1):
        question_code=random.randint(1,4)
        if question_code == 1:
            subtractquestion()
        elif question_code ==2:
            additionquestion()
        elif question_code == 3:
            divisionquestion()
        elif question_code==4:
            multiplyquestion()

# Main program starts here

main()

I'm trying to do a randomised math quiz and I have made it only do one question for times sake:

When I run this the program will repeat itself over an over again, even though I have only called upon a function once.(I understand this is probably very basic and messy but please have patience with me <3)

  • 1
    It works perfectly in my environment – Nyakiba Oct 10 '16 at 12:47
  • I just tried the code, it works absolutely fine. – Gurupad Mamadapur Oct 10 '16 at 12:47
  • 2
    By the way, `range(0,1)` is just `0`. So you don't need the for loop, you're only executing that part of the code once anyway – Farhan.K Oct 10 '16 at 12:49
  • You shoud take a look at [PEP 8](https://www.python.org/dev/peps/pep-0008/). Besides, your functions should return the attempts number (if you actually need it, you are not using `redos` for now) instead of using a global variable. – S. de Melo Oct 10 '16 at 12:51
  • `questions = (subtractquestion, additionquestion, divisionquestion, multiplyquestion)` And in the loop: `random.choice(questions)()` – S. de Melo Oct 10 '16 at 13:03
  • @Farkan.K It is intentionnal. OP mentions "I have made it only do one question for times sake" meaning that in normal situation it is intended to run the loop more than once (by swapping 0,1 for a bigger range). – jadsq Oct 10 '16 at 13:03
  • You want to be careful [comparing floats](http://stackoverflow.com/questions/26091689/python-floating-point-number-comparison) – CAB Oct 10 '16 at 13:09
  • Also, get in the habit of using '[if \_\_name__ == '\_\_main__':](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) – CAB Oct 10 '16 at 13:13
  • @jadsq Oh I didn't notice that – Farhan.K Oct 10 '16 at 13:24

0 Answers0