首先让我说我是新手,而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
答案 0 :(得分:1)
在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()