Ted Idzikowski - 16 days ago 7x

Python Question

I have a problem with my code, it works when all the user's answers are correct. Otherwise, let's say the number of questions the user wanted was 4 (n=4), and the user answered 3/4 correct, it gives you 2 times 4 **additional questions** and ends without the way expected (example: it jumps to next command >>>). Any ideas? Or any ways I could make this code functional?

`import random`

import math

def perform_test(int1,n):

score=0

if int1==0:

for y in range(n):

t1=random.randint(1,10)

t2=random.randint(1,10)

t3=int(t1+t2)

t4=int(t1*t2)

userAns1=int(input(str(t1)+' + '+str(t2)+' ='+' '))

if userAns1 != t3:

print('Incorrect - the answer is',t3)

else:

score = score + 1/n

return score

elif int1==1:

for y in range(n):

t1=random.randint(1,10)

t2=random.randint(1,10)

t3=int(t1+t2)

t4=int(t1*t2)

userAns2=int(input(str(t1)+' * '+str(t2)+' ='+' '))

if userAns2 != t4:

print('Incorrect - the answer is',t4)

else:

score = score + 1/n

return score

print('Welcome to the addition & multiplication test.\n \nHow many questions would like to be tested on?')

n=int(input('Enter a non negative integer for the answer: '))

if n == 0:

print('Good by')

else:

print('This software tests you with',n,'questions .....')

print('0) Addition \n1) Multiplication')

int1=int(input('Please make a selection (0 or 1): '))

if perform_test(int1,n) >= 0.8 :

print('Well done! Congratulations.')

if perform_test(int1,n) < 0.8 and perform_test(int1,n) > 0.6:

print('Not too bad but please study and practice some more.')

if perform_test(int1,n) < 0.6 :

print('Please study more and ask your teacher for help.')

Answer

The problem you have is that every time you evaluate the result of `perform_test(int1,n)`

it performs another test. Instead, get the result, and use that in the comparisons.
Also, you can chain inequalities in python, e.g. `0.8 > result >= 0.6`

. I assume the second case also includes a score of `0.6`

, otherwise a score of `0.6`

goes unhandled.

The modified code:

```
import random
import math
def perform_test(int1,n):
score=0
if int1==0:
for y in range(n):
t1=random.randint(1,10)
t2=random.randint(1,10)
t3=int(t1+t2)
t4=int(t1*t2)
userAns1=int(input(str(t1)+' + '+str(t2)+' ='+' '))
if userAns1 != t3:
print('Incorrect - the answer is',t3)
else:
score = score + 1/n
return score
elif int1==1:
for y in range(n):
t1=random.randint(1,10)
t2=random.randint(1,10)
t3=int(t1+t2)
t4=int(t1*t2)
userAns2=int(input(str(t1)+' * '+str(t2)+' ='+' '))
if userAns2 != t4:
print('Incorrect - the answer is',t4)
else:
score = score + 1/n
return score
print('Welcome to the addition & multiplication test.\n \nHow many questions would like to be tested on?')
n=int(input('Enter a non negative integer for the answer: '))
if n == 0:
print('Good-bye')
else:
print('This software tests you with',n,'questions .....')
print('0) Addition \n1) Multiplication')
int1=int(input('Please make a selection (0 or 1): '))
result=perform_test(int1,n)
if result >= 0.8:
print('Well done! Congratulations.')
if 0.8 > result >= 0.6:
print('Not too bad but please study and practice some more.')
if 0.6 > result:
print('Please study more and ask your teacher for help.')
```

Source (Stackoverflow)

Comments