首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'list index out of range‘错误的列表字典值

'list index out of range‘错误的列表字典值
EN

Stack Overflow用户
提问于 2018-05-05 12:56:48
回答 1查看 297关注 0票数 0

我创建了这个程序,“种植”西红柿,并为它们分配一个成熟日期(按轮换的单位)。但是,当我尝试使用该日期时,系统返回错误“list index out of range.‘”。我无论如何也不能理解为什么会发生这样的事情。请理解,我是一个初级程序员,所以当我真的在寻找这个问题的答案时,我发现的很多东西对我来说都是胡言乱语。谢谢!

代码语言:javascript
复制
#!/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))

(可能在某处有缩进错误,忽略这一点。我在将它作为代码放入堆栈溢出文本框时遇到了问题)

EN

回答 1

Stack Overflow用户

发布于 2018-05-05 13:13:06

在函数check_harvest()中,growing字典的长度可能与grown列表的长度不同。

plant()会将amount项目添加到growing,但是,只有一个项目会添加到grown。因此,如果amount大于1,则字典和列表的长度将会不同。

然后,check_harvest()迭代growing的长度,该长度可能比grown更长,这会导致尝试访问grown中超出列表限制的项。

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

代码语言:javascript
复制
            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循环中:

代码语言:javascript
复制
            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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50185847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档