[Tutor] Why won't it enter the quiz?

Adam adam.jtm30 at gmail.com
Mon Sep 19 23:24:32 CEST 2005


You're not returning the values of answer and guess so it jumps out of the 
while loop and does the else. Try this:

def add(a,b):
answer = a+b
guess = float(raw_input(a," + ",b," = "))
return answer, guess

answer, guess = add(num1,num2)
if guess != answer:

On 17/09/05, Nathan Pinno <falcon3166 at hotmail.com> wrote:
> 
> Hi all,
>  I've got this program I've written that should give an addition quiz, 
> except it never enters the quiz. How do I make it enter the quiz?
>  Here is the code:
> import random
>  def add(a,b):
> answer = a+b
> guess = float(raw_input(a," + ",b," = "))
>  num1 = random.choice(range(1,10))
> num2 = random.choice(range(1,10))
>  while 1:
> q = random.choice(range(15,31))
> cq = 1
> correct = 0
> while cq >= q:
> add(num1,num2)
> if guess != answer:
> print "Incorrect! The correct answer is: ",answer
> cq += 1
> elif guess == answer:
> print "Correct!"
> correct += 1
> cq += 1
> else:
> print "Questions: ",q
> print "Correct: ",correct
> print "Percent Correct: ",(cq/q)*100
> break
>  print "Goodbye."
>  It just prints out for an example:
>  Questions: 17
> Correct: 0
> Percent Correct: 0
> Goodbye.
>  Thanks,
> Nathan Pinno
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050919/08eeef2a/attachment.html


More information about the Tutor mailing list