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.