#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 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 )
嘿伙计们,我对python和编码整体都是全新的,但是想通过一个小小的测验来开始,虽然我觉得我在结构方面做了完全错误,如果我删除&# 39;得分'代码运行完全正常。如果我拥有它,就会发生这种情况:
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
在这种情况下,您能否给我任何提示,尤其是如何构建代码?非常感谢!
答案 0 :(得分:0)
在compare()
函数中,在尝试访问score
变量之前,您尚未定义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 )
变量。
你应该将它传递给compare函数(将它作为参数添加到函数中),然后返回结果(首选),或者将其声明为函数中的全局变量(更简单,但技术上不是最好的方法)。
例如
传递参数&amp;返回值解决方案:
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;
全局变量解决方案:
score
否则,全局范围中的score
与compare
范围内的options:{
scales: {
xAxes: [{ stacked:true}]
}
}
完全不同,即使它们具有相同的名称。
答案 1 :(得分:0)
在比较功能中,添加得分的全局声明:
Tess.setVariable("user_words_suffix", "user-words");