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
正如Bill Bell所指出的,您正试图使用
正确的方法之一是创建一个可以容纳12个元素的空列表。你知道吗
我散列出以下函数,其中没有任何信息。你知道吗
使基本的循环和数据输入为您提供基本的想法。你知道吗
工作代码
主()
输出/显示
注意:在进入
december
后,选择no
作为菜单选项,我将main()
函数重新运行以演示循环功能。你可以根据需要更换。你知道吗希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐