Python 我的代码已损坏(我是新手)UnboundLocalError:在赋值之前引用了局部变量'wood'
这是我的代码,由于某种原因它被破坏了。日志: -代码-Python 我的代码已损坏(我是新手)UnboundLocalError:在赋值之前引用了局部变量'wood',python,Python,这是我的代码,由于某种原因它被破坏了。日志: -代码- 在使用它们的每一个函数中,这些globals wood、clay、resourcecol都需要声明为global。至少在Python3中是这样,您似乎正在使用它 你应该考虑的是重新设计你是如何从游戏的一部分移动到另一部分的。据我所知,现在你通过一个直接呼叫到达那里,直到程序结束才会返回 每次调用都会在堆栈上存储前面的函数上下文,堆栈最终会填满。因此,就像@Mike所指出的那样,您试图以一种不太好的方式做一些好事。一种方法是将资源重新定义为所
在使用它们的每一个函数中,这些globals wood、clay、resourcecol都需要声明为global。至少在Python3中是这样,您似乎正在使用它 你应该考虑的是重新设计你是如何从游戏的一部分移动到另一部分的。据我所知,现在你通过一个直接呼叫到达那里,直到程序结束才会返回
每次调用都会在堆栈上存储前面的函数上下文,堆栈最终会填满。因此,就像@Mike所指出的那样,您试图以一种不太好的方式做一些好事。一种方法是将资源重新定义为所有人都可以访问的字典。希望下面的玩具代码能给你足够的机会重新设计你的方法。继续走
# You can access your dictionary anywhere
def play1():
print game_dict['wood']
def play2():
if game_dict['clay'] < 100:
print "You need some clay!"
game_dict['wood'] += 2
print "Now you have %d wood" % game_dict['wood']
# create a constructor for your game data, I called it game_dict, but
# there is nothing special about this name. Set various properties
# here that you can then modify in other functions
def initialize_game_dict(initial_wood, initial_clay):
local_dict = {'wood': ""}
local_dict = {'clay': ""}
local_dict['wood'] = initial_wood
local_dict['clay'] = initial_clay
return(local_dict)
game_dict = initialize_game_dict(10,25)
print "You are starting now with %d wood and %d clay" % (game_dict['wood'], game_dict['clay'])
play1()
play2()def play1():
print game_obj.wood
global farms
global wood
global clay
global resourcecol
wood=10
clay=10
def usernat():
rome='Rome'
usernation=input("Enter your nation: ")
def play5():
wood=0
clay=0
print("(Your wood and clay has been stolen by the local barbarians)")
print("Now we would want to get some resources, just so we could be safe from a crisis, when our farms will be destroyed.")
print("Let's collect resources, type wood or clay to send your men to collect the resources")
play6()
def play6():
resourcecol=input("Collect Wood/Clay or cancel? ")
if wood>100:
print("Your wood storages are full")
play6()
elif clay>100:
print("Your clay storages are full")
play6()
elif resourcecol=="wood":
wood=wood+10
print("Your men have successfully got ",wood, " wood.")
play6()
elif resourcecol=="clay":
clay=clay+10
print("Your men have successfully got ", clay, " clay.")
play6()
elif resourcecol=="cancel":
play7()
elif resourcecol=="Cancel":
play7()
elif resourcecol=="CANCEL":
play7()
elif resourcecol=="Clay":
clay=clay+10
print("Your men have successfully got ", clay, " clay.")
play6()
elif resourcecol=="CLAY":
clay=clay+10
print("Your men have successfully got ", clay, " clay.")
play6()
elif resourcecol=="Wood":
wood=wood+10
print("Your men have successfully got ",wood, " wood.")
play6()
elif resourcecol=="WOOD":
wood=wood+10
print("Your men have successfully got ",wood, " wood.")
play6()
else:
print("Command does not exist, please check spelling errors.")
play6()
def play7():
print("Play7 activated")
def play():
print("You have chosen to play as: ", usernation)
global troops
global gold
troops=0
gold=200
print("You currently have ", troops, "troops")
print("You currently have ", gold, "gold")
print("Since your nation needs immediate security, recruit at least '50' troops")
play2()
def play2():
troops=int(input("Recruit Troops: "))
global endgold
endgold=gold-2*troops
if endgold>=0:
if troops<50:
print("Your nation will not be secure with less than 50 troops, please recruit 50 or more.")
troops=0
play2()
print("You currently have ", troops, "troops and ", endgold, "Gold")
play3()
else:
print("You cannot recruit more troops than your funds can afford")
troops=0
play2()
def play3():
global wheat
wood=10
wheat=10
clay=10
farms=0
global farm_cons
farm_cons=clay+wood
print("You need more gold! Build some farms.")
print("You currently have avaiable resources for ", farm_cons/4, " farms.")
print("Build as many as you want from the avaiable.")
play4()
def play4():
farms=int(input("Build Farms: "))
global availablefarms
availablefarms=clay+wood/4
if farms<=availablefarms:
if farms<1:
print("You cannot build less than 1 farm")
play4()
if farms>availablefarms:
print("Your resources cannot afford to build ", farms, " farms")
play4()
print(farms, " farms are under construction.")
play5()
else:
print("Insufficient amount of farms")
play4()
if usernation=='Rome' :
play()
elif usernation=='rome' :
play()
elif usernation=='ROME' :
play()
else :
print("That nation does not exist")
return
print("Nations:")
print("Rome [Caesar]")
usernat();
print ("Values outside the function: ")
# You can access your dictionary anywhere
def play1():
print game_dict['wood']
def play2():
if game_dict['clay'] < 100:
print "You need some clay!"
game_dict['wood'] += 2
print "Now you have %d wood" % game_dict['wood']
# create a constructor for your game data, I called it game_dict, but
# there is nothing special about this name. Set various properties
# here that you can then modify in other functions
def initialize_game_dict(initial_wood, initial_clay):
local_dict = {'wood': ""}
local_dict = {'clay': ""}
local_dict['wood'] = initial_wood
local_dict['clay'] = initial_clay
return(local_dict)
game_dict = initialize_game_dict(10,25)
print "You are starting now with %d wood and %d clay" % (game_dict['wood'], game_dict['clay'])
play1()
play2()def play1():
print game_obj.wood