在使用while循环生成“list”索引器时,我遇到了这个错误:list赋值索引超出范围

2024-09-30 08:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

def main():

    endProgram = "no"

    #at the end the program will ask the user if the want to stop
    #if the say yes the program will end if they say no the will
    #cause the program to rerun
    while endProgram == "no":
        notGreenCost = [11]
        goneGreenCost = [11]
        savings = [11]
        months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

        #function calls
        notGreenCosts[12] = getNotGreen(notGreenCost, months)
        getGoneGreen(goneGreenCost, months)
        energySaved(notGreenCost, goneGreenCost, savings)
        displayInfo(notGreenCost, goneGreenCost, savings, months)

        endProgram = str(input("Do you want to end the program? yes or no"))

        def getNotGreen(notGreenCost, months):
            counter = 0
            while counter < 11:
                print("Enter GONE GREEN energy costs for", months[counter])
                notGreenCost[index] = int(input(":"))
                counter =  counter + 1


    return notGreenCost


main()

每次运行时,在第一个函数中重复while语句一次之后,它都会给出一个索引错误。你知道吗

Enter NOT GREEN energy costs for January

:2

Enter NOT GREEN energy costs for February

:3

回溯(最近一次呼叫):

File "/Users/jackdennison/Documents/9.5.py", line 32, in <module> main()

  File "/Users/jackdennison/Documents/9.5.py", line 15, in main
    notGreenCosts[12] = getNotGreen(notGreenCost, months)

  File "/Users/jackdennison/Documents/9.5.py", line 26, in getNotGreen
    notGreenCost[counter] = int(input(":"))

IndexError: list assignment index out of range

Tags: thetonoifmaincounterprogramwill
1条回答
网友
1楼 · 发布于 2024-09-30 08:15:54

正如Bill Bell所指出的,您正试图使用

notGreenCost = [11]
goneGreenCost = [11]
savings = [11]

正确的方法之一是创建一个可以容纳12个元素的空列表。你知道吗

notGreenCost = [None] * 12
goneGreenCost = [None] * 12
savings = [None] * 12

我散列出以下函数,其中没有任何信息。你知道吗

#getGoneGreen(goneGreenCost, months)
#energySaved(notGreenCost, goneGreenCost, savings)
#displayInfo(notGreenCost, goneGreenCost, savings, months)

使基本的循环和数据输入为您提供基本的想法。你知道吗

工作代码

import sys

def main():
def getNotGreen(notGreenCost, months):
        counter = 0
        while counter < 12:
            print("Enter GONE GREEN energy costs for", months[counter])
            notGreenCost[counter] = int(input(":"))
            counter =  counter + 1
        return notGreenCost

endProgram = "no"

#at the end the program will ask the user if the want to stop
#if the say yes the program will end if they say no the will
#cause the program to rerun
while endProgram == "no":
    notGreenCost = [None] * 12
    goneGreenCost = [None] * 12
    savings = [None] * 12
    months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

    #function calls
    notGreenCosts = []
    notGreenCosts = getNotGreen(notGreenCost, months)
    #getGoneGreen(goneGreenCost, months)
    #energySaved(notGreenCost, goneGreenCost, savings)
    #displayInfo(notGreenCost, goneGreenCost, savings, months)
    endProgram = str(raw_input("Do you want to end the program? yes or no:"))
    if endProgram == 'yes':
        print "notGreenCosts=",notGreenCosts
        sys.exit()
    else:
        main()                       

主()

输出/显示

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
('Enter GONE GREEN energy costs for', 'January')
:11
('Enter GONE GREEN energy costs for', 'February')
:12
('Enter GONE GREEN energy costs for', 'March')
:13
('Enter GONE GREEN energy costs for', 'April')
:14
('Enter GONE GREEN energy costs for', 'May')
:15
('Enter GONE GREEN energy costs for', 'June')
:16
('Enter GONE GREEN energy costs for', 'July')
:17
('Enter GONE GREEN energy costs for', 'August')
:18
('Enter GONE GREEN energy costs for', 'September')
:19
('Enter GONE GREEN energy costs for', 'October')
:20
('Enter GONE GREEN energy costs for', 'November')
:21
('Enter GONE GREEN energy costs for', 'December')
:22
Do you want to end the program? yes or no:yes
notGreenCosts= [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
>>> 

注意:在进入december后,选择no作为菜单选项,我将main()函数重新运行以演示循环功能。你可以根据需要更换。你知道吗

希望这有帮助。你知道吗

相关问题 更多 >

    热门问题