当在函数内更改变量值时,Python不保存变量值

时间:2020-07-22 10:27:03

标签: python python-3.x python-3.6

我相信我的变量是在程序开始时保存的,并允许函数访问它们,但我相信,但是当它们运行时,重复该函数时不会保存该值。

P1_score = 0
P2_score = 0
round_number = 0

def dice_rolling():
    # P1D1 means player ones dice one value and so on with P1D2
    import random
    # player ones turn
    print("player ones turn")
    P1D1 = random.randint(1, 6)
    print("your number is ", P1D1)
    P1D2 = random.randint(1, 6)
    print("your second number is", P1D2)
    # player twos turn
    print("player twos turn")
    P2D1 = random.randint(1, 6)
    print("your number is", P2D1)
    P2D2 = random.randint(1, 6)
    print("your second number is", P2D2)
    score_calculation(P1D1, P1D2, P2D1, P2D2,P1_score,P2_score,round_number)


def score_calculation(P1D1, P1D2, P2D1, P2D2,P1_score,P2_score,round_number):
    import random
    
    round_number = round_number + 1
    # player 1 score calculation
    total_P1 = P1D1 + P1D2
    P1_score = P1_score + total_P1
    if total_P1 % 2 == 0:
        P1_score = P1_score + 10
    else:
        P1_score = P1_score + 5
    if P1D1 == P1D2:
        P1D3 = random.randint(1, 6)
        P1_score = P1_score + P1D3

    # player 2 score calculation
    total_P2 = P2D1 + P2D2
    P2_score = P2_score + total_P2
    if total_P2 % 2 == 0:
        P2_score = P2_score + 10
    else:
        P2_score = P2_score + 5
    if P2D1 == P2D2:
        P2D3 = random.randint(1, 6)
        P2_score = P2_score + P2D3
    print("player ones score at the end of round", round_number, "is", P1_score)
    print("player twos score at the end of round",round_number,"is",P2_score)

    
for x in range(0,5):
    dice_rolling() 

我们将不胜感激,并且如果有人可以对我做错了什么以及要解决的问题给出简单的解释,那就太好了。

2 个答案:

答案 0 :(得分:0)

Python可以从函数内部的全局变量中读取内容,但是如果不做一些额外的工作就无法分配它们。通常,当您要使用全局变量时,最好通过在函数中使用global关键字使其明确:

my_global_var = 0

def some_function():
    global my_gobal_var
    
    my_global_var = 10

print(my_global_var) # it prints 10

somefunction() # modifies the global var

print(my_global_var) # now it prints 10

答案 1 :(得分:0)

变量在本地定义和使用。考虑这个例子。

data()
{
  return {
    filtration:
    [
      {
        key: 'categoryID',
        value: 2
      },
      {
        key: 'colorID',
        value: 2
      },
      {
        key: 'sizeID',
        value: 2
      },
    ]
  }
},
computed: {
                filteredProducts: function() {
                    const arrFilters = this.filtration;
                    return this.products.filter((product) => {
                        return arrFilters.every((filter) => {
                            return product[filter.key] === filter.value;
                        });
                    });             

                },

            }

结果为:

x = 1 #initial value
def test(x):
    print(x) #print what you got
    x += 1
    print(x) #print updated value

print(x) #Actual printouts here
test(x)
print(x)

如果希望变量保留在函数中,请考虑使用类变量或全局变量。 (当您遇到更复杂的问题时,我建议避免使用全局变量)

全局示例:

1 
1 #value when entering the function
2 #Value changed by function
1 #value outside the function did not change

结果:

global x
x = 1
def test():
    global x
    x+=1
print(x)
test()
print(x)
test()
print(x)

最后的类变量:

1
2
3