Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Why doesn't the following program enter the quiz?

falcon3166falcon3166 Posts: 22Member
Why doesn't the following code enter the quiz?

import random

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

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:
answer, guess = 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."

Here is a sample output when I run it:
Questions: 27
Correct: 0
Percent Right: 0
Goodbye!

How do I make it enter the quiz?
Swat spam before it fills your inbox!

Comments

  • DrostDrost Posts: 24Member
    : Why doesn't the following code enter the quiz?
    :
    : import random
    :
    : def add(a,b):
    : answer = a+b
    : guess = float(raw_input([red]a," + ",b," = "[/red]))
    : return answer, guess
    :
    : num1 = random.choice(range(1,10))
    : num2 = random.choice(range(1,10))
    :
    : [red]while 1:[/red]
    : q = random.choice(range(15,31))
    : cq = [red]1[/red]
    : correct = 0
    : while cq >= q:
    : answer, guess = add(num1,num2)
    : if guess != answer:
    : print "Incorrect! The correct answer is: ",answer
    : cq += 1 [red]#either way it gets executed[/red]
    : elif guess == answer: [red]#at this point it can only be equal[/red]
    : print "Correct!"
    : correct += 1
    : cq += 1
    : else:
    : print "Questions: ",q
    : print "Correct: ",correct
    : print "Percent Correct: ",[red](cq/q)[/red]*100 [red]#correct/cq and it needs a typecast to get a float result[/red]
    : break [red]#not needed[/red]
    :
    : print "Goodbye."
    :
    : Here is a sample output when I run it:
    : Questions: 27
    : Correct: 0
    : Percent Right: 0
    : Goodbye!
    :
    : How do I make it enter the quiz?
    : Swat spam before it fills your inbox!
    :

    I highlighted the mistakes...
    Hope that I got what you would've liked to accomplish with it.
    Something like this:
    [code]
    import random

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

    q = random.choice(range(3,8))
    cq = 0
    correct = 0
    while cq <= q:
    num1 = random.choice(range(1,10))
    num2 = random.choice(range(1,10))
    answer, guess = add(num1,num2)
    if guess != answer:
    print "Incorrect! The correct answer is: ",answer
    else:
    print "Correct!"
    correct += 1
    cq += 1
    else:
    print "Questions: ",cq
    print "Correct: ",correct
    print "Percent Correct: %3.2f" %((float(correct)/cq)*100)

    print "Goodbye."
    [/code]

    Drost
  • falcon3166falcon3166 Posts: 22Member
    : : Why doesn't the following code enter the quiz?
    : :
    : : import random
    : :
    : : def add(a,b):
    : : answer = a+b
    : : guess = float(raw_input([red]a," + ",b," = "[/red]))
    : : return answer, guess
    : :
    : : num1 = random.choice(range(1,10))
    : : num2 = random.choice(range(1,10))
    : :
    : : [red]while 1:[/red]
    : : q = random.choice(range(15,31))
    : : cq = [red]1[/red]
    : : correct = 0
    : : while cq >= q:
    : : answer, guess = add(num1,num2)
    : : if guess != answer:
    : : print "Incorrect! The correct answer is: ",answer
    : : cq += 1 [red]#either way it gets executed[/red]
    : : elif guess == answer: [red]#at this point it can only be equal[/red]
    : : print "Correct!"
    : : correct += 1
    : : cq += 1
    : : else:
    : : print "Questions: ",q
    : : print "Correct: ",correct
    : : print "Percent Correct: ",[red](cq/q)[/red]*100 [red]#correct/cq and it needs a typecast to get a float result[/red]
    : : break [red]#not needed[/red]
    : :
    : : print "Goodbye."
    : :
    : : Here is a sample output when I run it:
    : : Questions: 27
    : : Correct: 0
    : : Percent Right: 0
    : : Goodbye!
    : :
    : : How do I make it enter the quiz?
    : : Swat spam before it fills your inbox!
    : :
    :
    : I highlighted the mistakes...
    : Hope that I got what you would've liked to accomplish with it.
    : Something like this:
    : [code]
    : import random
    :
    : def add(a,b):
    : answer = a+b
    : guess = float(raw_input("%d + %d = " % (a, b)))
    : return answer, guess
    :
    : q = random.choice(range(15,31))
    : cq = 0
    : correct = 0
    : while cq <= q:
    : num1 = random.choice(range(1,10))
    : num2 = random.choice(range(1,10))
    : answer, guess = add(num1,num2)
    : if guess != answer:
    : print "Incorrect! The correct answer is: ",answer
    : else:
    : print "Correct!"
    : correct += 1
    : cq += 1
    : else:
    : print "Questions: ",cq
    : print "Correct: ",correct
    : print "Percent Correct: %3.2f" %((float(correct)/cq)*100)
    :
    : print "Goodbye."
    : [/code]
    :
    : Drost
    :
    Thanks, now it works perfectly! Now I know how to fix my big program as well!
    Swat spam before it fills your inbox!

Sign In or Register to comment.