我对Python很陌生。我正在尝试创建一个自我评分测验。到目前为止我只有两个问题。我试图为这个人得到的数字以及这个人出错的问题数量分配一个全局变量。如果键入了所有正确的答案,程序将正常工作;但是,任何不正确的答案都会因为不正确的定义而回复。
def test():
print "We are going to take a small quiz"
print "Lets start with what your name."
name = raw_input("What is your name?")
correct = [0]
incorrect = [0]
def q1():
print "What organization did Hellboy work for?"
answer = raw_input("Type your answer.").lower()
if answer == "bprd" or answer == "b.p.r.d":
print "Your are correct."
correct[0] += 1
else:
print "That is wrong."
incorret[0] += 1
q1()
def q2():
print "What is the name of Captain America's sidekick?"
answer = raw_input("Your answer please.").lower()
if answer =="bucky barnes" or answer == "falcon":
print "Your are right"
correct[0] += 1
else:
print "Sorry that in incorrect"
incorrect[0] += 1
q2()
print "Good job %s, you got %r questions right and %r questions wrong" % (name, correct, incorrect)
raw_input()
test()
答案 0 :(得分:2)
由于第一个问题中此行的拼写错误,您收到了未定义的错误:
incorret[0] += 1
纠正这个并且代码应该有效。
答案 1 :(得分:-1)
如果将其设置为类,则可能更容易。
class classTest():
def __init__():
print "We are going to take a small quiz"
print "Lets start with what your name."
self.name = raw_input("What is your name?")
self.correct = 0
self.incorrect = 0
def q1(self):
print "What organization did Hellboy work for?"
answer = raw_input("Type your answer.").lower()
if answer == "bprd" or answer == "b.p.r.d":
print "Your are correct."
self.correct += 1
else:
print "That is wrong."
self.incorrect += 1
def q2(self):
print "What is the name of Captain America's sidekick?"
answer = raw_input("Your answer please.").lower()
if answer =="bucky barnes" or answer == "falcon":
print "Your are right"
self.correct[0] += 1
else:
print "Sorry that in incorrect"
self.incorrect[0] += 1
def end(self):
print "Good job %s, you got %r questions right and %r questions wrong" % (self.name, self.correct, self.incorrect)
test = new classTest()
test.q1()
test.q2()
test.end()