Question: Python beginner - Am I doing it right? Quiz with scores

Question

Python beginner - Am I doing it right? Quiz with scores

Answers 2
Added at 2016-12-16 05:12
Tags
Question
#Quiz answer variables go here!
ca1 = cringe_crew[0]
ca2 = cucks[0]
ca3 = 5
ca4 = cringe_crew[1]
ca5 = 'No'

#Questions
q1 = "Question 1 - Who do we refer to when shouting CODE YELLOW? "
q2 = "Question 2 - Who would you think of if I called him 'Linux Boy': "
q3 = "Question 3 - How many people were there in the original Cringe-Crew back in the days of 2015? "
q4 = "Question 4 - Biggest pseudo Overwatch fan? (top tip: even bought a fucking bag): "
q5 = "Question 5 - Do we miss Luke Slater? "

#a is the raw_input answer
#b is the correct answer needed
#a1-a5 will be separately stored raw_input ANSWERS
#ca1-ca5 are already called as variables and these are the correct answers.

#Score

score = 0

def compare( a, ca ):
    if a == ca:
        score = score + 1
        print "That's correct! The answer was %s" % ca
        print "Your score is", score

    else:
        print "%s is not the correct answer - you did not get a point!" % a
    return;


#test
a1 = raw_input(q1)
compare( a1, ca1 )
a2 = raw_input(q2)
compare( a2, ca2 )
a3 = raw_input(q3)
compare( a3, ca3 )
a4 = raw_input(q4)
compare( a4, ca4 )
a5 = raw_input(q5)
compare( a5, ca5 )

Hey guys, I am completely new to python and coding overall, but wanted to kick off with a little quiz with a score, although I feel like I went off doing it completely wrong structure-wise, if i remove the 'scores' the code runs completely fine. If I have it, this happens:

Traceback (most recent call last):
  File "ex2.py", line 57, in <module>
    compare( a1, ca1 )
  File "ex2.py", line 46, in compare
    score = score + 1
UnboundLocalError: local variable 'score' referenced before assignment

Can you give me any tips especially on how to structure the code in such case? Many thanks!

Answers
nr: #1 dodano: 2016-12-16 05:12

Within your compare() function, you have not defined your score variable before you try to access it's value.

You should either pass it to the compare function (add it as a parameter to the function), and then return the result back (preferred), or declare it as a global variable within the function (simpler, but not technically the best approach).

Eg

Pass parameter & return value solution:

def compare( a, ca, scr ):
    if a == ca:
        scr = scr + 1
        print "That's correct! The answer was %s" % ca
        print "Your score is", scr 

    else:
        print "%s is not the correct answer - you did not get a point!" % a
    return scr ;

# skipping some code here

score = compare( a1, ca1, score )

Global variable solution:

def compare( a, ca ):
    global score
    if a == ca:
        score = score + 1
        print "That's correct! The answer was %s" % ca
        print "Your score is", score

    else:
        print "%s is not the correct answer - you did not get a point!" % a
    return;

Otherwise, the score in your global scope is a completely different variable to the score in your compare scope, even though they have the same name.

nr: #2 dodano: 2016-12-16 05:12

In the compare function add the global statement for score:

def compare(a, ca):
    global score
    if ca == ca:
        # the rest of your code
Source Show
◀ Wstecz