Python 拒绝将变量添加到
所以我对python和一般的编码都是相当陌生的,我决定制作一个基于文本的琐事游戏作为一种测试。我已经为第一个问题编写了所有代码。我将为每个问题重复的代码。我的问题具体在第10-11行。预期的功能是向当前分数添加一个,然后打印ScoreGovered变量,该变量使用格式告诉您分数。但它不起作用。变量仍然可以很好地打印,但分数变量没有添加到,保持为零Python 拒绝将变量添加到,python,variables,Python,Variables,所以我对python和一般的编码都是相当陌生的,我决定制作一个基于文本的琐事游戏作为一种测试。我已经为第一个问题编写了所有代码。我将为每个问题重复的代码。我的问题具体在第10-11行。预期的功能是向当前分数添加一个,然后打印ScoreGovered变量,该变量使用格式告诉您分数。但它不起作用。变量仍然可以很好地打印,但分数变量没有添加到,保持为零 TRIVIA = input('TRIVIA: press enter to start') strike = int('3') strikeslef
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()
我可能会使用类似于:
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 1
scoreGovered
不是一个函数,它是一个您分配但不更新的变量。这将是一个功能的好地方,您可以随时重复使用它来打印分数。例如:
def print_score(score):
print('Your score is {}'.format(score))
您可以随时重复使用此功能来打印分数。您可以打印获得的分数,而不是分数。未更新已获得的分数。字符串只是在赋值时创建的,如果值发生更改,它不会自行更新。但是,分数正在更新。注释:
strike=int('3')
?“正常”语法有什么问题:(strike=3
)?将scoregovered=('Your score is{}')。将(score)
格式设置为score=score+1
或更简洁的score+=1
。