变量拒绝添加到

时间:2019-02-06 18:32:50

标签: python variables

因此,我一般对python和编码尚不熟悉,因此我决定制作一个基于文本的琐事游戏作为一种测试。我已经为第一个问题编写了所有代码。我将对每个问题重复的代码。我的问题特别是在10-11行上。预期的功能是在当前分数上添加一个,然后打印使用格式告诉您分数的scoregained变量。但这不起作用。该变量仍然可以正常打印,但未添加score变量,仍为零。

from numpy import nan, nanmean
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

A = [[  7,     4,   5,  7000],
     [  1,   900,   9,   nan],
     [  5, -1000, nan,   100],
     [nan,   nan,   3,  1000]]

scaler.fit(A)

In [45]: scaler.mean_
Out[45]: array([4.33333333,  -32.,    5.66666667, 2700.])

In [46]: scaler.transform(A)
Out[46]: array([[ 1.06904497,  0.04638641, -0.26726124,  1.40399977],
                [-1.33630621,  1.20089267,  1.33630621,         nan],
                [ 0.26726124, -1.24727908,         nan, -0.84893009],
                [        nan,         nan, -1.06904497, -0.55506968]])

In [54]: nanmean(scaler.transform(A), axis=0)
Out[54]: array([ 1.48029737e-16,  0.00000000e+00, -1.48029737e-16,0.00000000e+00])

2 个答案:

答案 0 :(得分:3)

(function() { 'use strict'; if (!window.location.href.match(/f=%20license:%22Creative\+Commons\+0%22/i)) { window.location.href += '&f=%20license:%22Creative+Commons+0%22'; } })(); 不是函数,它是您分配但不更新的变量。这对于功能来说是个好地方,您可以在要打印乐谱时随时重复使用。例如:

scoregained

您可以在希望打印分数时随时重用此功能。

答案 1 :(得分:0)

我可能会使用类似的东西:

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