Reputation: 23
so i'm fairly new to python and coding in general and i decided to make a text based trivia game as a sort of test. and I've coded everything for the first question. code which i will repeat for each question. my problem is specifically on lines 10-11. the intended function is to add one to the current score, then print the scoregained variable which uses format to tell you the score. but it doesn't work. the variable still prints fine but the score variable isn't added to, remaining at zero.
TRIVIA = input('TRIVIA: press enter to start')
strike = int('3')
strikesleft = ('strikes left: {} ').format(strike)
score = int('0')
scoregained = ('Your score is {}' ).format(score)
Q1 = input('What is the diameter of the earth? ')
if Q1 == ('7917.5'):
print('correct!')
input()
score = score+1
print(scoregained)
input()
Upvotes: 2
Views: 49
Reputation: 824
scoregained
isn't a function, it is a variable you assign but do not update. This would be a great place for a function, which you can reuse whenever you want to print the score. For example:
def print_score(score):
print('Your score is {}'.format(score))
You can reuse this function anytime you wish to print the score.
Upvotes: 3
Reputation: 98921
I'd probably use something like:
def score_stats(score):
print('Your score is {}'.format(score))
input('TRIVIA: press enter to start')
score, strike = 0, 3
strikesleft = 'strikes left: {}'.format(strike)
score_stats(score)
Q1 = input('What is the diameter of the earth?')
if Q1 == '7917.5':
print('correct!')
score += 1
score_stats(score)
else:
print('incorrect!')
score_stats(score)
Q2...
Output:
TRIVIA: press enter to start
Your score is 0
What is the diameter of the earth? 7917.5
correct!
Your score is 1
Upvotes: 0