-1

I'm having trouble with my python code I have been trying to make a maths quiz for my GCSE but I have run across a problem.

The return function is not returning any variables as you can see below I have stated the variables that need to be 'returned' unless I am using the wrong function.

My aim is to have the numgen generate the numbers and the numbers to be then used in the def question to ask the user for the answer and then the def correct to tell the user to if the question is correct.

import random
import time
Decision = 'neither'
print("\n\n\n\n\n\n")

Name = input("Hello what is your name?")

print("...")

time.sleep(1)

print("Hello",Name,"are you ready for the maths quiz?")

while Decision.lower() != "yes" or "no":
    Decision = input("Type either\n'yes'\nor\n'no'")

    if Decision.lower() == "yes":
        print("Ok, we will proceed")
        break

    elif Decision == "no":
        print("Please come back when you are ready")
        exit(0)

    else:
        print("please type again either 'yes' or 'no'")

marks = 0

def numgen():
    num1 = random.randint(1,40)
    numlist = random.choice(['*','/','+','-'])
    num2 = random.randrange(2,20,2)
    answer = eval(str(num1) + numlist + str(num2))
    return(num1, numlist, num2, answer)

score = 0

def question (num1, numlist,num2, answer):
    print("This question is worth 10 marks.")
    print ("The question is:",num1, numlist, num2)
    Q1 = input('What is your answer?')
    Q1 = float(Q1)
    return(Q1)

def correct(Q1):
    if Q1 == answer:
        print("Well done you got it right.")
        score = score + 10
    else:
        print("you were incorrect the asnwer was:",answer)
        return (score)

questions = 0
while questions < 10:
    numgen()
    question(num1,num2,answer,numlist)
    correct(Q1)


print(marks)

EDIT: Okay i thank everyone for your help but im still having problems because in this line print ("The question is:",num1, numlist, num2) where num2 is, is where for some reason the answer appears i dont know what causes this but it is very annoying can anyone help. This is after i edited the code to include

num1,num2,numlist,answer=numgen()
Q1=question(num1,num2,answer,numlist)
score = int(score)
score = correct(score, Q1)

so for example if I had:

the question is: 24 + 46

the answer would be 46. Should I give up on using the def command? thanks in advance for your help.

  • it seem you only have a order issue with the parameters, i edited my answer with further explanation – DorElias Sep 30 '15 at 19:48

3 Answers3

0
num1, num2, answer, numlist = numgen()

this will work, as you return something but you were not assigning the returned values to anything so that they could be used after.

This is called unpacking.

It is exactly like

a, b = 1, 3

You can also use tuples to switch variables values:

a, b = b, a
DevLounge
  • 8,313
  • 3
  • 31
  • 44
0

well you are not using the values returned from the numgen function. try changing the last part of your program to this:

while questions < 10:
    num1,num2,answer,numlist = numgen() # here you save the output of numgen
    question(num1,num2,answer,numlist) # and then use it
    correct(Q1)

edit:

well after a deepr look on your code, it seems you dont understand scopes. have a look at this Short Description of the Scoping Rules?

the idea is that a variable have a "place" where it is defiened and can be accessed. when you define a variable in a function (inside def) it is not automaticly accessible from a diffrent method. so in your case for example the function correct(Q1) cant see the variable answer as it was defined in numgen and it wasnt passed to it as an argument and it is not a "global" variable

EDIT:

your problem now is the order of the parameters,

you call it like:

question(num1,num2,answer,numlist)

but it is defiened like:

def question (num1, numlist,num2, answer):

see the diffrence in the order? they should be in the same order

Community
  • 1
  • 1
DorElias
  • 2,243
  • 15
  • 18
  • Thank You for helping so quickly i do understand the basics of scoping but the link you gave helped me understand more. im sure it'll help in my course ^-^. i feel stupid now that i see the answer GG – oliver imms Sep 22 '15 at 14:05
0

Well you need to store the returned values somewhere:

while questions < 10:
   num1, numlist, num2, answer = numgen()
   Q1 = question(num1,num2,answer,numlist)
   correct(Q1)