局部变量“Body”在赋值之前引用 - 代码中抛出错误,但是无法将原因视为“Body”;被定义
此代码抛出“赋值前引用的局部变量‘Body’”错误,该错误被标记在“for x in body”上。我查看了代码,我真的很难找到错误的原因,因为该问题与我使用相同格式创建的其他条件不一致?
bgs = ['01_BG_Blue.png', '02_BG_Red.png', '03_BG_Green.png', '04_BG_Grey.png',
'05_BG_Orange.png', '06_BG_Silver.png', '07_BG_Navy.png', '08_BG_Cave.png', '09_BG_Forest
Background.png']
Body = ['01_Body_Wolf.png', '02_Body_Green Wolf.png','03_Body_Ice Wolf.png']
Outfit = ['00_Outfit_Fighter.png', '01_Outfit_Spartan.png', '02_Outfit_Black belt
Fighter.png']
Weapons = ['00_Weapons_Sword 1.png', '01_Weapons_Axe.png' '02_Weapons_Quiver.png'
'03_Weapons_Sword 2.png' '04_Weapons_Axe 2.png']
mouths = ['01_Mouth_Tongue.png', '02_Mouth_Snarl.png']
eyez = ['00_Eyes_Brown eyes.png', '01_Eyes_Grey Eyes.png', '02_Eyes_Red Eyes.png',
'03_Eyes_White Eyes.png', '04_Eyes_Snake Eyes.png', '05_Eyes_Future Eyes.png']
Traits = ['00_Traits_Earring 1.png', '01_Traits_Earring 2.png', '02_Traits_Laural.png',
'03_Traits_Pipe.png', '04_Traits_Scar.png', '05_Traits_Crown.png']
Fweapons = ['00_Overhead_Bow.png', '01_Overhead_Spear.png']
def gen():
count = 0
while count != 5555: # size of collection, so script stops
traits = []
traits_names = []
"""
Just some checks to avoid issues coming up in the big lists of traits.
"""
for x in backgroundsList:
if len(x) < 2:
backgroundsList.remove(x)
for x in BodyList:
if len(x) < 2:
BodyList.remove(x)
for x in outfitList:
if len(x) < 2:
outfitList.remove(x)
for x in WeaponsList:
if len(x) < 2:
WeaponsList.remove(x)
for x in mouthsList:
if len(x) < 2:
mouthsList.remove(x)
for x in eyesList:
if len(x) < 2:
eyesList.remove(x)
for x in TraitsList:
if len(x) < 2:
TraitsList.remove(x)
for x in FrontweaponsList:
if len(x) < 2:
FrontweaponsList.remove(x)
"""
Collecting of the randomly chosen traits.
"""
bgcheck = random.choice(backgroundsList)
backgroundsList.remove(bgcheck)
for x in bgs:
if x.split('.png')[0].replace('_', ' ').replace(' ', '_', 2).split('_')[2] ==
bgcheck:
bg = x
break
Bodycheck = random.choice(BodyList)
BodyList.remove(Bodycheck)
for x in Body:
if x.split('.png')[0].replace('_', ' ').replace(' ', '_', 2).split('_')[2] ==
Bodycheck:
Body = x
break**
This code is throwing up 'local variable 'Body' referenced before assignment' error which is being flagged on the 'for x in body'. I have looked at the code and I am really struggling to find the cause of the error as the issue isn't consistent with the other conditions I have created using the same format?
bgs = ['01_BG_Blue.png', '02_BG_Red.png', '03_BG_Green.png', '04_BG_Grey.png',
'05_BG_Orange.png', '06_BG_Silver.png', '07_BG_Navy.png', '08_BG_Cave.png', '09_BG_Forest
Background.png']
Body = ['01_Body_Wolf.png', '02_Body_Green Wolf.png','03_Body_Ice Wolf.png']
Outfit = ['00_Outfit_Fighter.png', '01_Outfit_Spartan.png', '02_Outfit_Black belt
Fighter.png']
Weapons = ['00_Weapons_Sword 1.png', '01_Weapons_Axe.png' '02_Weapons_Quiver.png'
'03_Weapons_Sword 2.png' '04_Weapons_Axe 2.png']
mouths = ['01_Mouth_Tongue.png', '02_Mouth_Snarl.png']
eyez = ['00_Eyes_Brown eyes.png', '01_Eyes_Grey Eyes.png', '02_Eyes_Red Eyes.png',
'03_Eyes_White Eyes.png', '04_Eyes_Snake Eyes.png', '05_Eyes_Future Eyes.png']
Traits = ['00_Traits_Earring 1.png', '01_Traits_Earring 2.png', '02_Traits_Laural.png',
'03_Traits_Pipe.png', '04_Traits_Scar.png', '05_Traits_Crown.png']
Fweapons = ['00_Overhead_Bow.png', '01_Overhead_Spear.png']
def gen():
count = 0
while count != 5555: # size of collection, so script stops
traits = []
traits_names = []
"""
Just some checks to avoid issues coming up in the big lists of traits.
"""
for x in backgroundsList:
if len(x) < 2:
backgroundsList.remove(x)
for x in BodyList:
if len(x) < 2:
BodyList.remove(x)
for x in outfitList:
if len(x) < 2:
outfitList.remove(x)
for x in WeaponsList:
if len(x) < 2:
WeaponsList.remove(x)
for x in mouthsList:
if len(x) < 2:
mouthsList.remove(x)
for x in eyesList:
if len(x) < 2:
eyesList.remove(x)
for x in TraitsList:
if len(x) < 2:
TraitsList.remove(x)
for x in FrontweaponsList:
if len(x) < 2:
FrontweaponsList.remove(x)
"""
Collecting of the randomly chosen traits.
"""
bgcheck = random.choice(backgroundsList)
backgroundsList.remove(bgcheck)
for x in bgs:
if x.split('.png')[0].replace('_', ' ').replace(' ', '_', 2).split('_')[2] ==
bgcheck:
bg = x
break
Bodycheck = random.choice(BodyList)
BodyList.remove(Bodycheck)
for x in Body:
if x.split('.png')[0].replace('_', ' ').replace(' ', '_', 2).split('_')[2] ==
Bodycheck:
Body = x
break**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在全局范围(函数外部)声明了所有列表和变量。因此,除非将它们声明为全局变量,否则无法从函数内部访问它们。
只需在函数
gen
的开头添加这一行:添加要在函数中使用的所有变量名称。
You've declared all the lists and variables in the global scope(outside the function). So they cannot be accessed from within the function unless you declare them as global variables.
Just add this line at the beginning of the function
gen
:Add all the variable names that you want to use in the function.