Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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 列表-字典值出现“列表索引超出范围”错误_Python_Python 3.x_List_Dictionary - Fatal编程技术网

Python 列表-字典值出现“列表索引超出范围”错误

Python 列表-字典值出现“列表索引超出范围”错误,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,我创建了一个“种植”西红柿的程序,并以轮次为单位指定西红柿的成熟日期。但是,当我尝试使用该日期时,系统返回错误“列表索引超出范围”。我一辈子都无法理解为什么会发生这种情况。请理解,我是一个初学者程序员,所以虽然我确实在寻找这个问题的答案,但我发现的很多东西对我来说完全是胡言乱语。谢谢 #!/usr/bin/python # -*- coding: utf-8 -*- # this code is meant for testing a portion of farmboi simulator.

我创建了一个“种植”西红柿的程序,并以轮次为单位指定西红柿的成熟日期。但是,当我尝试使用该日期时,系统返回错误“列表索引超出范围”。我一辈子都无法理解为什么会发生这种情况。请理解,我是一个初学者程序员,所以虽然我确实在寻找这个问题的答案,但我发现的很多东西对我来说完全是胡言乱语。谢谢

#!/usr/bin/python
# -*- coding: utf-8 -*-
# this code is meant for testing a portion of farmboi simulator.
# this code is not the entire program.

tomato_seeds = 2
tomato_planted = 0
tomato = 0
tomato_id = 0
growing = {}  # keeps track of what items are currently growing
grown = []
turn = 0  # keeps track of what turn the player is on


# actually 'plants' the tomatoes

def plant(crop, amount):  # the error only runs when amount > 1
    global tomato_seeds
    global tomato_planted
    global tomato_id
    global growing
    global grown
    if crop == 'tomato':
        for i in range(amount):
            if tomato_seeds > 0:
                tomato_seeds -= 1
                tomato_planted += 1
                growing['tomato_' + str(tomato_id)] = turn + 5  
                # this creates a library that contains tomatoes and their turn of harvest
                grown.append('tomato_' + str(tomato_id))  
                # this list is so I can run a for loop using numbers (there's probably a better way to do this idk)
                tomato_id += 1
    else:
        print('You do not have any tomato seeds remaining\n')
        print(str(grown))
        print(str(growing))


# checks every loop to see if the tomatoes are ripe

def check_harvest():
    global tomato
    global tomato_planted
    harvested = 0
    for item in range(len(growing)):
        print('checker: ' + str(growing[grown[item]]))
        if growing[grown[item]] == turn:  
        # Im calling the value here the checker (this is also where the error occurrs)
            tomato += 1
            tomato_planted -= 1
            del growing[grown[item]]
            del grown[item]
        if harvested > 0:
            print('You have harvested ' + str(harvested) + ' tomatoes')


while True:
    if input('What would you like to do?\n').lower() == 'plant':
        plant('tomato', 2)

    check_harvest()
    turn += 1
    print('turn: ' + str(turn))
某个地方可能有缩进错误,请忽略这一点。我在将其放入堆栈溢出文本框时遇到了问题,因为函数检查中的代码

\u正在增长的字典的长度可能与已增长列表的长度不同

植物会向生长中添加数量项目,但只会向生长中添加一个项目。因此,如果amount大于1,字典和列表的长度将发生分歧

然后,check_harvest在可能比grown长的grown长度上进行迭代,这会导致尝试访问超出列表限制的grown中的项目

如果将两行缩进,可能会解决问题:

            grown.append("tomato_" + str(tomato_id)) #this list is so I can run a for loop using numbers (there's probably a better way to do this idk)
            tomato_id += 1
使它们位于for循环中:

            for i in range(amount):
                if tomato_seeds > 0:
                    tomato_seeds -= 1
                    tomato_planted += 1
                    growing["tomato_" + str(tomato_id)] = turn + 5 #this creates a library that contains tomatoes and their turn of harvest
                    grown.append("tomato_" + str(tomato_id)) #this list is so I can run a for loop using numbers (there's probably a better way to do this idk)
                    tomato_id += 1

但是我不能确定,因为我不太理解你的代码。

你能在这里写下错误吗?回溯上一次调用:File C:/Users/cavem/Documents/Python/Personal Python Files/Farmboi Simulator/Farmboi test.py,第52行,在check\u harvest File C:/Users/cavem/Documents/Python/Personal Python Files/Farmboi Simulator/Farmboi test.py的第38行,在check\u harvest printchecker:+strgrowing[grown[item]]索引器中:列出索引range@JakeShropshire我可以毫无问题地运行你的代码。这两个功能已充电,while循环运行正常。超出范围是在我第六次运行工厂时出现的。请看随附的图片。缩进错误是我复制到堆栈溢出的问题。我想我不明白你的意思,字典和列表没有以同样的速度增长/消耗。我认为.append方法的作用与在字典中声明一个新项相同,我很可能是错的。我对使用del函数有点担心,那么问题是否存在?@JakeShropshire:那么你应该修正你问题中的缩进。这个错误可以用您发布的代码来解释,但是如果您运行的不是这个代码,那么进一步猜测是没有效果的。此外,应将异常添加到问题中,并将其正确格式化为代码。