Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何阻止这些变量重置,同时仍能循环它们?_Python - Fatal编程技术网

Python 如何阻止这些变量重置,同时仍能循环它们?

Python 如何阻止这些变量重置,同时仍能循环它们?,python,Python,以下是我的部分代码: def dice_game(): dice_window = Tk() dice_window.geometry('450x450') player1_score = 0 player2_score = 0 def player1_turn(): player1_num1 = random.randint(1, 6) player1_num2 = random.randint(1, 6)

以下是我的部分代码:

def dice_game():
     dice_window = Tk()
     dice_window.geometry('450x450')
     player1_score = 0 
     player2_score = 0
     def player1_turn():
         player1_num1 = random.randint(1, 6)
         player1_num2 = random.randint(1, 6)
         player1_score = player1_num1 + player1_num2
         player1_total = player1_num1 + player1_num2
         if (player1_total % 2) == 0:
             player1_score = player1_score + 10
         else:
             player1_score = player1_score - 5
         player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
         player1_result.pack()
     for x in range(1, 6):
         player1_turn()

我曾尝试将循环放入player1_turn()函数和dice_game()函数中,但结果总是一样的。如何保持两名玩家的分数,而不在本节每次循环5次时重置它们?

好吧,代码本身在骰子游戏()函数中有for循环。 如您所见,您正在该函数中将分数声明为零。因此,要调用for循环,需要调用dice_game(),它会再次将分数重置为零

也许您应该尝试在函数dice_game()之外使用该for循环,最后,分数将被保留。或者你也可以接受声明

player1_score = 0 
player2_score = 0

在函数外部,作为全局变量。

好吧,代码本身在dice\u game()函数中有for循环。 如您所见,您正在该函数中将分数声明为零。因此,要调用for循环,需要调用dice_game(),它会再次将分数重置为零

也许您应该尝试在函数dice_game()之外使用该for循环,最后,分数将被保留。或者你也可以接受声明

player1_score = 0 
player2_score = 0

在函数外部,作为全局变量。

由于您在
player1\u得分
player2\u得分
函数体内部分配
player1\u得分和player2\u得分
函数的局部变量
player1\u得分

因此,如果您使用的是
python3
,则可以使用
nonlocal
关键字将
player1\u分数
player2\u分数
定义为非局部变量

代码如下所示

def dice_game():
     dice_window = Tk()
     dice_window.geometry('450x450')
     player1_score = 0 
     player2_score = 0
     def player1_turn():
         nonlocal player1_score, player2_score
         player1_num1 = random.randint(1, 6)
         player1_num2 = random.randint(1, 6)
         player1_score = player1_num1 + player1_num2
         player1_total = player1_num1 + player1_num2
         if (player1_total % 2) == 0:
             player1_score = player1_score + 10
         else:
             player1_score = player1_score - 5
         player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
         player1_result.pack()
     for x in range(1, 6):
         player1_turn()

# END

对于Python2,有一种解决方法,您可以将
player1_分数和
player2_分数
存储在类似
score={'player1_分数:0',player2_分数:0}

的dict中,因为您正在
player1_分数
player2_分数
分配到
函数体中,它使函数的
player1\u分数和player2\u分数
局部变量
player1\u-turn()

因此,如果您使用的是
python3
,则可以使用
nonlocal
关键字将
player1\u分数
player2\u分数
定义为非局部变量

代码如下所示

def dice_game():
     dice_window = Tk()
     dice_window.geometry('450x450')
     player1_score = 0 
     player2_score = 0
     def player1_turn():
         nonlocal player1_score, player2_score
         player1_num1 = random.randint(1, 6)
         player1_num2 = random.randint(1, 6)
         player1_score = player1_num1 + player1_num2
         player1_total = player1_num1 + player1_num2
         if (player1_total % 2) == 0:
             player1_score = player1_score + 10
         else:
             player1_score = player1_score - 5
         player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
         player1_result.pack()
     for x in range(1, 6):
         player1_turn()

# END

对于Python2,有一种变通方法,您可以将
player1\u分数和player2\u分数存储在一个dict中,比如
score={'player1\u分数:0,'player2\u分数:0}
让我们把它分解一下。我可以看到
player1\u turn()
是嵌套的,因为您想访问
dice\u窗口。但是有更好的办法

首先,将功能分开
player1_turn
需要一个骰子窗口,并且您想要保存的东西似乎是
player1_score
,因此我们将返回该值

def player1_turn(dice_window):
    player1_num1 = random.randint(1, 6)
    player1_num2 = random.randint(1, 6)
    player1_score = player1_num1 + player1_num2
    player1_total = player1_num1 + player1_num2
    if (player1_total % 2) == 0:
        player1_score = player1_score + 10
    else:
        player1_score = player1_score - 5
    player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
    player1_result.pack()
    return player1_score
现在进入游戏:

def dice_game():
    dice_window = Tk()
    dice_window.geometry('450x450')

    # we're not using x, so we should make it clear that this is a loop of 5
    scores = []
    total = 0
    for x in range(5):
        player1_score = player1_turn(dice_window)
        # now what to do with the score?
        scores.append(player1_score)  # save it?
        print(player1_score)  # print it?
        total += player1_score  # add it up
    print(scores)
    print(total)

让我们来分析一下。我可以看到
player1\u turn()
是嵌套的,因为您想访问
dice\u窗口。但是有更好的办法

首先,将功能分开
player1_turn
需要一个骰子窗口,并且您想要保存的东西似乎是
player1_score
,因此我们将返回该值

def player1_turn(dice_window):
    player1_num1 = random.randint(1, 6)
    player1_num2 = random.randint(1, 6)
    player1_score = player1_num1 + player1_num2
    player1_total = player1_num1 + player1_num2
    if (player1_total % 2) == 0:
        player1_score = player1_score + 10
    else:
        player1_score = player1_score - 5
    player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
    player1_result.pack()
    return player1_score
现在进入游戏:

def dice_game():
    dice_window = Tk()
    dice_window.geometry('450x450')

    # we're not using x, so we should make it clear that this is a loop of 5
    scores = []
    total = 0
    for x in range(5):
        player1_score = player1_turn(dice_window)
        # now what to do with the score?
        scores.append(player1_score)  # save it?
        print(player1_score)  # print it?
        total += player1_score  # add it up
    print(scores)
    print(total)

查找有关嵌套函数作用域的更多信息。然后,你必须增加分数,而不是每次都重新初始化。这将重置该值。创建一个并将其添加到您的帖子中。查找有关嵌套函数范围的更多信息。然后,你必须增加分数,而不是每次都重新初始化。这将重置该值。创建一个并将其添加到您的帖子中。那么我应该如何将其添加到我的代码中?就让他们一个接一个吗?是的,一个接一个。如果“现在开始游戏:”已经从我的帖子中删除了。它有点有效。我会稍微调整一下,但你给我的已经解决了我的核心问题。谢谢!那么,我应该如何把这些放在我的代码中呢?就让他们一个接一个吗?是的,一个接一个。如果“现在开始游戏:”已经从我的帖子中删除了。它有点有效。我会稍微调整一下,但你给我的已经解决了我的核心问题。谢谢!