Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 UnboundLocalError:赋值前引用的局部变量“gold”_Python - Fatal编程技术网

Python UnboundLocalError:赋值前引用的局部变量“gold”

Python UnboundLocalError:赋值前引用的局部变量“gold”,python,Python,现在,在你决定因为我没有阅读其他问题而把我扔出窗外之前,我已经找了这么长时间了。事实上,我已经找到了很多答案,但是它们在不同的环境中,我真的不知道如何将它们与我的代码一起使用,因此我得出结论,与其浪费时间在其他问题上,而这些问题不会给我问题的答案,不如询问我所遇到的确切错误,这对我更有利 import time import random inventory = [] gold = 0 rawfish = ["Mackarel", "Cod", "Salmon", "Herring", "Tun

现在,在你决定因为我没有阅读其他问题而把我扔出窗外之前,我已经找了这么长时间了。事实上,我已经找到了很多答案,但是它们在不同的环境中,我真的不知道如何将它们与我的代码一起使用,因此我得出结论,与其浪费时间在其他问题上,而这些问题不会给我问题的答案,不如询问我所遇到的确切错误,这对我更有利

import time
import random
inventory = []
gold = 0
rawfish = ["Mackarel", "Cod", "Salmon", "Herring", "Tuna"]
trash = ["Old Shoe", "Thin Bone", "Rusted Empty Box", "Plank Fragment"]
special = ["Copper Ring"]

print"  _           _       _       _        ______ _     _     _ "            
print" | |         | |     (_)     ( )      |  ____(_)   | |   (_)"            
print" | |     ___ | |_ __  _ _ __ |/ ___   | |__   _ ___| |__  _ _ __   __ _ "
print" | |    / _ \| | '_ \| | '_ \  / __|  |  __| | / __| '_ \| | '_ \ / _` |"
print" | |___| (_) | | |_) | | | | | \__ \  | |    | \__ \ | | | | | | | (_| |"
print" |______\___/|_| .__/|_|_| |_| |___/  |_|    |_|___/_| |_|_|_| |_|\__, |"
print"               | |                                                 __/ |"
print"               |_|                                                |___/ "
time.sleep(2)
print".       .                                     /|    /|     "
print" \     /                o                      |     |     "
print"  \   /   .-. .--..--.  .   .-. .--.       o   |     |     "
print"   \ /   (.-' |   `--.  |  (   )|  |           |     |     "
print"    '     `--''   `--'-' `- `-' '  `-      o   |  o  |     " 
time.sleep(2)
print "In this current version the first item in your inventory is sold."

def sell_function():
    if inventory[0] in rawfish:
        sold = inventory.pop(0)
        gold = gold+5
        print "You have sold a", sold, "for 5 gold coins!"
    elif inventory[0] in trash:
        sold = inventory.pop(0)
        gold = gold+5
        print "You have recycled a", sold, "for 1 gold coins!"
    elif inventory[0] in special:
        sold = inventory.pop(0)
        gold = gold+5
        print "You have sold a", sold, "for 10 gold coins!"
    else:
        print "Shopkeeper:'You can't sell that.'"

def fish_function():
    random_fishingchance = random.randrange(1,32)
    if 1 <= random_fishingchance < 3:
        inventory.append("Mackarel")
        print "You have reeled in a Mackarel!"
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 3 <= random_fishingchance < 5:
        inventory.append("Cod")
        print "You have reeled in a Cod!"
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 5 <= random_fishingchance < 7:
        inventory.append("Salmon")
        print "You have reeled in a Salmon!"
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 7 <= random_fishingchance < 9:
        inventory.append("Herring")
        print "You have reeled in a Herring!"
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 9 <= random_fishingchance < 11:
        inventory.append("Tuna")
        print "You have reeled in a Tuna!"
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 11 <= random_fishingchance < 16:
        inventory.append("Old Shoe")
        print "You have reeled in an Old Shoe..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 16 <= random_fishingchance < 21:
        inventory.append("Thin Bone")
        print "You have reeled in a Thin Bone..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 21 <= random_fishingchance < 26:
        inventory.append("Rusted Empty Box")
        print "You have reeled in a Rusted Empty Box..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 26 <= random_fishingchance < 31:
        inventory.append("Plank Fragment")
        print "You have reeled in a Plank Fragment..."
        time.sleep(0.5)
        print "You place it into your inventory"
    elif 31 <= random_fishingchance < 32:
        inventory.append("Copper Ring")
        print "You have reeled in a ring shaped object covered in mud."
        print "After cleaning it you notice it is a Copper Ring!"
        time.sleep(0.5)
        print "You place it into your inventory"
    else:
        print "It seems your fishing line has snapped!"


def action_function():
    while True:
        action = raw_input("Do you want to 'sell'  'fish'  'inventory' 'money' or 'quit'?")
        if action == "quit":
            break
            end
        if action == "sell":
            sell_function()
        if action == "fish":
            print "You throw your reel..."
            time.sleep(10)
            fish_function()
        if action == "inventory":
            print "You begin to open your inventory"
            time.sleep(0.5)
            print inventory
        if action == "money":
            print gold
        if action == "gold":
            print gold
action_function()
由于这一行gold=gold+5,python认为gold是一个局部变量,当您实际调用函数sell_时,它不会从全局变量gold获取gold的值

这是因为函数中的局部变量是在解析函数定义时确定的

如果要修改全局变量,请使用全局语句:

def sell_function():
    global gold
    if inventory[0] in rawfish:
       sold = inventory.pop(0)
       gold = gold+5

审查职能范围。因为您要为gold分配一个新值,所以python会创建一个新变量。然后尝试将其递增5,因此它尝试获取新“gold”的值,从而抛出错误

UnboundLocalError: local variable 'gold' referenced before assignment
在所有更改“gold”的函数中将其声明为全局变量

global gold
if inventory[0] in rawfish:

这里出现了一些严重的打印问题……还有一些严重的目录/列表缺陷迹象……当我写“global gold=gold+5”时,它惊呼着语法error@Lolpin你读过答案中的密码了吗?必须有一行只包含全局gold,将gold声明为全局变量。之后,执行gold=gold+5将如您所期望的那样工作。@Lolpin只需在函数顶部添加一行全局gold,然后正常使用gold。对不起,我在尝试之前已经发表了评论。谢谢你的时间:沙经过更多的学习,你会知道环球是个坏主意。谢谢,我现在明白了:D
global gold
if inventory[0] in rawfish: