PythonNooby PythonNooby - 4 years ago 159
Python Question

My Program is not printing what I want it to print

I have just finished this code , added my final print line and now suddenly when I test it , it doesn't print out what I want it to print.

import random

name=input("Welcome to this Arithmetic quiz,please enter your name:")

score = 0

for i in range(10):
number1=random.randint(20,50)

number2=random.randint(1,20)
oper=random.choice('+-*')
correct_answer = eval(str(number1)+oper+str(number2))
answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer)
if answer == correct_answer:
print('Correct!')
score +=1
else:
print('Incorrect!')
print("You got",score,"out of 10")


When I ever I give the right answer it still gives me Incorrect leading it to tell me that I got 0/10.

Answer Source

To match the symptoms reported I believe that your code is actually:

import random

name=input("Welcome to this Arithmetic quiz,please enter your name:")

score = 0

for i in range(10):
    number1=random.randint(20,50)

    number2=random.randint(1,20)
    oper=random.choice('+-*')
    correct_answer = eval(str(number1)+oper+str(number2))
    answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer)
    if answer == correct_answer:
       print('Correct!')
       score +=1
    else:
       print('Incorrect!')
print("You got",score,"out of 10")

Note the indentation of the else statement is aligned with the if, not the for. Given that this line:

answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer

assigns a boolean to the variable answer, not the answer that the user entered. You can do 2 things:

remove the == correct_answer part resulting in:

answer = int(input('What is:'+str(number1)+oper+str(number2)+'='))

or

change the if statement to:

if answer:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download