[英]How do I keep track of score within the functions of my python trivia game?
Let me start by saying that I am new to this and python is my first language so the simpler you can answer the better!首先让我说我是新手,python 是我的第一语言,所以你能回答的越简单越好! Obviously, I cant put score += 1 into the conditional branch of each question's function because of errors with scope.
显然,由于范围错误,我不能将 score += 1 放入每个问题函数的条件分支中。 How would I go about keeping track of the score?
我将如何跟踪分数? Would I use another function for score itself?
我会使用另一个函数来评分吗?
Here is my code:这是我的代码:
answer_a = ['A', 'a']
answer_b = ['B', 'b']
answer_c = ['C', 'c']
answer_d = ['D', 'd']
score = 0
def question1():
print('What state contains the Statue of Liberty?'
'\nA. California\nB. Rhode Island\nC. New York\nD. Florida')
choice = input('> ')
if choice in answer_c:
print('\nCORRECT!\n')
question2()
if choice in answer_a:
print('Incorrect.\n')
question2()
if choice in answer_b:
print('Incorrect.\n')
question2()
if choice in answer_d:
print('Incorrect.\n')
question2()
else:
print('Please select a valid input\n')
question1()
def question2():
pass
def question3():
pass
In Python, you can use global variables to keep track of values such as scores that keep on incrementing.在 Python 中,您可以使用全局变量来跟踪值,例如不断增加的分数。
answer_a = ['A', 'a']
answer_b = ['B', 'b']
answer_c = ['C', 'c']
answer_d = ['D', 'd']
score = 0
def question1():
global score #global variable
print('What state contains the Statue of Liberty?'
'\nA. California\nB. Rhode Island\nC. New York\nD. Florida')
choice = input('> ')
if choice in answer_c:
print('\nCORRECT!\n')
question2()
score+=1
elif choice in answer_a:
print('Incorrect.\n')
question2()
elif choice in answer_b:
print('Incorrect.\n')
question2()
elif choice in answer_d:
print('Incorrect.\n')
question2()
else:
print('Please select a valid input\n')
question1()
print("total score is", score)
def question2():
pass
def question3():
pass
question1()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.