[英]Python Global Variable Not Working
I have an extremely long program for a Canasta game that I am writing, in it there is a variable that ends the turn of the player if it equals a certain value. 我正在编写的Canasta游戏有一个非常长的程序,其中有一个变量,如果变量等于某个值,该变量将结束玩家的回合。 However, the turn repeats itself.
但是,转弯会重复。
I have read many different articles and posts on forums like this one, but none of these seem to work. 我在类似这样的论坛上阅读了许多不同的文章和帖子,但这些似乎都没有用。 I even have a debug system to print the value of the variable before the if statement, and the variable is the right value, but the if statement doesn't see the same value.
我什至有一个调试系统,可以在if语句之前打印变量的值,并且该变量是正确的值,但是if语句看不到相同的值。
I can't give the entire code, as it is extremely long, but I can give the sections that use this variable. 我不能给出整个代码,因为它很长,但是我可以给出使用该变量的部分。 I am using Python 2.7.2 and cannot make files separate from the main file to import.
我正在使用Python 2.7.2,无法将文件与要导入的主文件分开。
The first line of the program is: 该程序的第一行是:
endTurn=1
The following function is called when conditions to win the game are met, but I have not been able to test that this works because of the bug. 当满足赢得游戏的条件时,将调用以下函数,但是由于该错误,我无法测试该函数是否有效。
def winGame(player):
global endTurn
gameWinner=player
gameWinner["score"]+=100
endTurn=0
The following function is called when a move is made. 进行移动时将调用以下函数。 The variable
decide
is a raw_input()
variable. decide
的变量是raw_input()
变量。
def move(player,decide):
global endTurn
theMove=decide.lower()
#if player says to end turn
if theMove=="end":
#until player discards something
discarded=0
while not discarded:
displayHand(player)
#ask player for discard
discard=int(raw_input(" Enter the list number of the card you wish to discard:"))-1
#if discard ID is in player's hand length
if not discard<0 and not discard>len(player["hand"])-1:
#add card to discard pile and remove from hand
discardPile.append(player["hand"][discard])
del(player["hand"][discard])
discarded=1
debug("Before changing, endTurn is %s"%str(endTurn))
endTurn = 0
debug("After changing, endTurn is %s"%str(endTurn))
if theMove=="new book":
newBook(player)
if theMove=="add to book":
addBook(player)
Here is where the turn should be ended. 这是转弯应该结束的地方。
turn(thePlayer)
repeats the function. turn(thePlayer)
重复该功能。 The debug statement shows the correct value, 0, but the if
still reads a 1. The function this is in also has the global endTurn
at the top. debug语句显示正确的值0,但是
if
仍然读取1。此函数还在顶部具有global endTurn
。
debug("If ending turn, endTurn of %s should be 0."%str(endTurn))
if endTurn==1:
turn(thePlayer)
Any help is greatly appreciated! 任何帮助是极大的赞赏!
EDIT: The code is available at http://labs.codecademy.com/CV9z#:workspace . 编辑:该代码位于http://labs.codecademy.com/CV9z#:workspace 。 I ask that anyone viewing the code does not modify it, so that other people can see the true code.
我要求查看该代码的任何人都不要对其进行修改,以便其他人可以看到真实的代码。
I fixed the bug. 我已修复该错误。 I removed the
endTurn
variable and instead made the function that ends the turn just do the turn of the next player. 我删除了
endTurn
变量,而是制作了结束回合的功能,只是进行下一个玩家的回合。 It also simplified my code a bit. 这也简化了我的代码。
I am wondering though, is using this method of running a function inside of itself over and over and over again (without leaving) rather messy or maybe slowing down my program? 我想知道,是否正在使用这种方法在自身内部一遍又一遍地运行一个函数(不离开),却很杂乱,或者可能会使我的程序变慢?
EDIT: I now realize that this answer does not exactly help with anyone else who is stuck with global variables... 编辑:我现在意识到,这个答案并不能完全帮助那些陷入全局变量的人。
The previous answer I posted was just wrong, as it was based on a misinterpretation of the namespace docs as I had read them (Thanks to TheifMaster for pointing out my error), so this is a SEVERELY EDITED ANSWER: 我发布的上一个答案是错误的,因为它是基于我阅读它们时对名称空间文档的误解(感谢TheifMaster指出了我的错误),所以这是一个经过严格编辑的答案:
The problem is in the while loop that I was able to read when the link to the entire code was posted after the erroneous answer I gave earlier. 问题出在while循环中,当我在前面给出的错误答案后发布了指向整个代码的链接时,我能够读取它。
while gameWinner==0:
endTurn=1
turn(player1)
if not gameWinner==0:
endTurn=1
turn(player2)
The game can never get to player two in this loop until gameWinner!=0. 直到gameWinner!= 0,游戏才能在此循环中到达第二位玩家。 I added the entire code to my sandbox and changed it to:
我将整个代码添加到沙箱中,并将其更改为:
while gameWinner==0:
endTurn=1
turn(player1)
if gameWinner==0: #if player1 did not win yet
endTurn=1
turn(player2)
However, the game is still buggy... No one can win! 但是,游戏仍然存在错误……没有人能赢! gameWinner is not changed globally by the winGame() function until you add it to the global statement as I have shown here.
除非您将它添加到global语句中,否则不会通过winGame()函数来全局更改gameWinner,如我在此处所示。
def winGame(player):
global endTurn, gameWinner
gameWinner=player
gameWinner["score"]+=100
debug("gameWinner == " + str(gameWinner))
endTurn=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.