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! Obviously, I cant put score += 1 into the conditional branch of each question's function because of errors with scope. 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.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.