需要Python 3.6/3.2中程序的帮助吗

需要Python 3.6/3.2中程序的帮助吗,python,stored-procedures,Python,Stored Procedures,我和我的朋友正在学校做一个项目,我们需要一些程序方面的帮助。我们知道如何创建和调用一个插件,但由于某些原因,它不能与我们当前的代码一起工作 每当我们试图对代码的某些部分使用该过程时,就会出现以下错误: Traceback (most recent call last): File "F:\board game edited.py", line 78, in <module> P1() File "F:\board game edited.py", line 53, i

我和我的朋友正在学校做一个项目,我们需要一些程序方面的帮助。我们知道如何创建和调用一个插件,但由于某些原因,它不能与我们当前的代码一起工作

每当我们试图对代码的某些部分使用该过程时,就会出现以下错误:

Traceback (most recent call last):
  File "F:\board game edited.py", line 78, in <module>
    P1()
  File "F:\board game edited.py", line 53, in P1
    counter1 = counter1 + dicetotal
UnboundLocalError: local variable 'counter1' referenced before assignment
代码如下:

import random

counter1 = 0
counter2 = 0

print("***********************************BOARD GAME**********************************")
print(" ")
print("***********************************GAME RULES**********************************")
print(" ")
print("----------------------------Game is for 2 players only-------------------------")
print(" ")
print(">The game board is a 7x7 board going up to 49")
print(" ")
print("43 44 45 46 47 48 49")
print("42 41 40 39 38 37 36")
print("29 30 31 32 33 34 35")
print("28 27 26 25 24 23 22")
print("15 16 17 18 19 20 21")
print("14 13 12 11 10 9  8 ")
print("1  2  3  4  5  6  7 ")
print(" ")
print(">The objective is to be the first player to reach space 49")
print(" ")
print(">There are 2 die, if you roll the same number twice, you will go back 
the number of spaces you rolled")
print(" ")
print(">If you land on spaces 27, 31 and 47, you will go back to space 24")
print(" ")
print(">Press ENTER to play")
input()

print("**********************************START 
GAME***********************************")
input()

print("Starting positions for both players = 0")
print(" ")

with open("Game Messages.txt","r") as infile:
    data = infile.read()
    my_list = data.splitlines()

def P1():
    print(my_list[0])
    dice1 = random.randint(1,6)
    print("dice 1 =",dice1)
    dice2 = random.randint(1,6)
    print("dice 2 =",dice2)
    dicetotal = dice1 + dice2
    print("dice total =",dicetotal)
    if dice1 == dice2:
        counter1 = counter1 - dicetotal
        print(my_list[1])
    else:
        counter1 = counter1 + dicetotal
    if counter1 >= 49:
        print("P1 space = 49")
    if counter1 <= 0:
        print("P1 space = 0")
    if counter1 <=49:
        print("P1 space =",counter1)
    if counter1 == 47:
        counter1 = 24
        print(my_list[2])
    if counter1 == 27:
        counter1 = 24
        print(my_list[3])
    if counter1 == 31:
        counter1 = 24
        print(my_list[4])
    if counter1 >= 49:
        print(" ")
        print(my_list[5])
        print(" ")
        print("Press ENTER to exit the game")
        input()
        exit()
    input()

def P2():
    print(my_list[6])
    dice1 = random.randint(1,6)
    print("dice 1 =",dice1)
    dice2 = random.randint(1,6)
    print("dice 2 =",dice2)
    dicetotal = dice1 + dice2
    print("dice total =",dicetotal)
    if dice1 == dice2:
        counter2 = counter2 - dicetotal
        print(my_list[7])
    else:
        counter2 = counter2 + dicetotal
    if counter2 >= 49:
        print("P2 space = 49")
    if counter1 <= 0:
        print("P2 space = 0")
    if counter1 <= 49:
        print("P2 space =",counter2)
    if counter2 == 47:
        counter2 = 24
        print(my_list[8])
    if counter2 == 27:
        counter2 = 24
        print(my_list[9])
    if counter2 == 31:
        counter2 = 24
        print(my_list[10])
    if counter2 >= 49:
        print(" ")
        print(my_list[11])
        print(" ")
        print("Press ENTER to exit the game")
        input()
        exit()
    input()

P1()
P2()

如果您以任何方式帮助我们,我们将不胜感激,谢谢

消息告诉您发生了什么

if dice1 == dice2:
    counter1 = counter1 - dicetotal
    print(my_list[1])
else:
    counter1 = counter1 + dicetotal
启动此代码时,计数器1没有指定值。您试图将骰子卷添加到一个不存在的数量。在到达这里之前,必须给计数器1一个初始值

如果要使用并更新具有该名称的外部变量,则需要行

global counter1
在你的功能的顶端


因此,ed需要您提供影响问题的所有代码。告诉我们有一条你没有发布的临界线是一种诱饵和交换——不公平-

该函数中未定义局部变量counter1。您必须在函数中为其赋值,使其成为全局变量,或将其传入。

在if/else语句中,您有counter1=counter1。调试时,等式右侧的计数器1的值是多少?它将是计数器1以前的值。例如,如果以前的值是14,而模具都是4,那么新的值将是6。计数器1的起始值为0,但已在代码的前面部分中分配。根据您对其他答案的评论,您应该显示实际分配计数器1的位置。我们不能确定你是否一直犯着小错误。你提供的代码表明你没有,我们不能假设你没有向我们展示什么。请阅读中的更多内容。当你得到一个解决方案时,请记住向上投票,并接受你最喜欢的答案,即使你必须自己写,这样堆栈溢出可以正确地存档问题。另外,请编辑你在MCVE上的帖子。这只是代码的一部分,之前我分配了counter1的值。无论如何谢谢你!你发布了整个函数?我可以一直看到这个范围的顶部,而您还没有初始化计数器1。当您使用该变量时,它是一个局部值。在函数之外初始化的任何东西都是不相关的变量。我使用了全局计数器1行,它工作了!非常感谢你!!我在代码的前一部分中分配了它,在过程之前没有显示。我必须在内部分配它吗?是的,要么在函数上方声明它,而不仅仅是在首先执行的代码中,要么将它传递到函数中,将函数更改为p11