[英]How do I keep track of score within the functions of my python trivia game?
首先讓我說我是新手,python 是我的第一語言,所以你能回答的越簡單越好! 顯然,由於范圍錯誤,我不能將 score += 1 放入每個問題函數的條件分支中。 我將如何跟蹤分數? 我會使用另一個函數來評分嗎?
這是我的代碼:
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
在 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.