所以我对python和编码还是个新手,我决定做一个基于文本的琐事游戏作为一种测试。我已经为第一个问题编写了所有代码。我将为每个问题重复的代码。我的问题特别是在第10-11行。预期的功能是在当前分数上加1,然后打印获得分数的变量,该变量使用format来告诉您分数。但它不起作用。变量仍然打印得很好,但是score变量没有被添加,仍然是零。
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()发布于 2019-02-07 02:49:23
scoregained不是一个函数,它是一个你赋值但不更新的变量。对于函数来说,这是一个很好的地方,您可以在想要打印分数时重用它。例如:
def print_score(score):
print('Your score is {}'.format(score))您可以随时重复使用此函数来打印比分。
发布于 2019-02-07 02:43:05
我可能会使用类似这样的东西:
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...输出:
TRIVIA: press enter to start
Your score is 0
What is the diameter of the earth? 7917.5
correct!
Your score is 1https://stackoverflow.com/questions/54560326
复制相似问题