[Tutor] Will the following code ever exit?
Alan G
alan.gauld at freenet.co.uk
Sat Sep 17 10:50:15 CEST 2005
Nathan,
> Will the following code ever exit,
Yes, it will exit - with an error because it can never run.
def add(a,b):
answer = a+b
guess = float(raw_input(a," + ",b," = "))
You never return the answer so it dies when the function dies.
num1 = random.choice(range(1,10))
num2 = random.choice(range(1,10))
q = random.choice(range(15,31)
cq = 1
while cq >= q:
and cq is 1 and q is between 15,31 so cq will never be >= to q,
the while loop will never execute.
add(num1,num2)
if guess != answer:
But if it did answer is not defined 9see comment above)
so this will crash.
else:
print "Questions: ",q
print "Correct: ",correct
And if it gets this far correct is not defined so again
we get an error.
You need to return answer from the function and define
correct/incorrect. Then turn the while test into a <=
Then it might start to run although not necessarily correctly.
Alan G.
More information about the Tutor
mailing list