0

As a novice to Python myself, I encountered an error when trying to develop a Quiz program. I found that when the program generates two random numbers to add together, and the user attempts to enter in the correct values for the question, the program trips up and still prints that the input from the user is invalid.

def quiz():
    print("The quiz will begin shortly")
    print(numberone)
    print("+")
    print(numbertwo)
    answerone=input("Answer:")
    if answerone==(numberone + numbertwo):
        print("Correct")
        score+1
        print("You're score is", score, ".")
    else:
        print("Incorrect")
        print(numberone+numbertwo)

I do not understand what I am doing wrong, so any help would be much appreciated.

(NOTE: 'numberone' and numbertwo' are both defined)

MackM
  • 2,906
  • 5
  • 31
  • 45

4 Answers4

0

You'll either need to define numberone and numbertwo in the function quiz:

def quiz():
    numberone = 6
    numbertwo = 3   # etc.

Or, pass them as parameters:

def quiz(numberone, numbertwo):
    print numberone   #etc
David Zemens
  • 53,033
  • 11
  • 81
  • 130
0

Your issue is that input from your user is a string. You will need to convert it to an integer before you can compare it.

int(answerone)

so try this:

def quiz():
    print("The quiz will begin shortly")
    print(numberone)
    print("+")
    print(numbertwo)
    answerone=input("Answer:")
    if int(answerone)==(numberone + numbertwo):
        print("Correct")
        score += 1
        print("You're score is {}.".format(str(score)))
    else:
        print("Incorrect")
        print(numberone+numbertwo)
Martin Lear
  • 262
  • 1
  • 7
0

You forgot to assign score while incrementing

is:

    print("Correct")
    score+1
    print("You're score is", score, ".")

should be:

    print("Correct")
    score += 1
    print("You're score is", score, ".")
wasiek
  • 66
  • 3
0

There are bunch of problems in your code. Let's see one by one.

  • Your indentation is all wrong. You should first fix it.
  • numberone, numbertwo and score can't be accessed under this scope. One good idea is to pass them as function parameter.

Like this:

def quiz(numberone,numbertwo,score):
  • As answerone should be an integer, cast the input as int

Like this:

answerone=int(input("Answer:")) #if using python3     
  • You are adding 1 with score, but aren't assigning it back.

Use score=score+1 or score+=1 rather score+1

  • Return score from the function to use the updated value of score for next call.

So, the working code can be like this:

def quiz(numberone,numbertwo,score):
    print("The quiz will begin shortly")
    print(numberone)
    print("+")
    print(numbertwo)
    answerone=int(input("Answer:"))
    if answerone==(numberone + numbertwo):
        print("Correct")
        score += 1
        print("You're score is", score, ".")
    else:
        print("Incorrect")
        print(numberone+numbertwo)
    return score

You can call it like below:

score=0
while(score<10):
    score=quiz(someRandNumber,anotherRandNumber,score)
else:
    print "Your score is 10" 
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57