我在做一个简单的琐事游戏。我在下面提示用户并以交互方式显示问题。根据条件从运行总量中增加或减少一个变量
我想添加一个“分数”功能。每当我尝试初始化“count”为0或类似的内容时,我的Question
类,并增加value
中存储的值,count
停留在0.我在这里遇到问题。理想情况下,我想在用户回答每个问题后打印分数。如果正确,则将self.value
添加到count
,否则将其相减。
import random
class Question(object):
def __init__(self, question, answer, value):
self.question = question
self.answer = answer
self.value = value
def ask(self):
print (self.question + "?")
count = 0
response = input().strip()
if response in self.answer:
x = random.randint(0,3)
print (positives[x])
print ("Answer is" + " " + self.answer)
else:
y = random.randint(0,3)
print (negatives[y])
print ("Answer is" + " " + self.answer)
question_answer_value_tuples = [('This author made a University of Virginia law professor the protagonist of his 2002 novel "The Summons"',
'(John) Grisham']
#there are a few hundred of these. This is an example that I read into Question. List of tuples I made from a jeopardy dataset.
positives = ["Correct!", "Nice Job", "Smooth", "Smarty"]
negatives = ["Wrong!", "Think Again", "Incorrect", "So Sorry" ]
questions = []
for (q,a,v) in question_answer_value_tuples:
questions.append(Question(q,a,v))
print ("Press any key for a new question, or 'quit' to quit. Enjoy!")
for question in questions:
print ("Continue?")
choice = input()
if choice in ["quit", "no", "exit", "escape", "leave"]:
break
question.ask()
我想添加类似
count = 0
if response in self.answer:
count += self.value
else:
count -= self.value
print (count)
我觉得我在与局部/全局变量的麻烦。
如果你可以有计数作为类,即一部分的单人游戏的事'self.count = 0' 或者,如果你想使用全局, – Nullman
感谢声明为全球'全球count',我认为以下方法最适合我的需求和理解! –