本文介绍了河内塔,索引错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做着名的游戏河内之塔,当我运行我的代码时出错。因此,当我运行它时,在第17行引发一个IndexError:list index超出范围,这是add_method中的if new_disk< = self.content [0]:。

我看到了如果值不在列表中或超出范围,可以引发Indexerror,但我不知道在我的情况下有什么问题。这是我的代码:



我尝试过:



i'm doing the famous game "Tower of Hanoi" and i have an error when i run my code. So, when i run it raise an IndexError: list index out of range, in line 17, which is " if new_disk <= self.content[0]:" in the add_method.
I saw that an Indexerror can be raised if the value aren't in the list or out of the range, but i don't know what's the problem in my case. Here's my code:

What I have tried:

class HanoiT():

    def __init__(self, content=None):
        if content is None:
            self.content=[]
        else:
            self.content=content

    def addmethod(self, new_disk): #Add an element on the beginning of the list
        if new_disk <= self.content[0]:
            self.content.insert(0, new_disk)
        else:
            raise ValueError("No disk may be placed on top of a smaller disk")


    def deletemethod(self): #Delete the first element of a list
        self.content.pop(0)

    def affichage(self): #Method to print the list which represent the "disk" putted on the tower from the top to bottom
        for i in self.content:
            print(i, end=" ")

    def movedisk(self, Tsource, Ttarget):
        Ttarget.addmethod(Tsource.content[0])
        Tsource.deletemethod()

    def movetower(self, N, Tour1, Tour3, Tour2):
        if N >= 1:
            self.movetower(N-1, Tour1, Tour2, Tour3)
            self.movedisk(Tour1, Tour3)
            self.movetower(N-1, Tour2, Tour3, Tour1)




N = 4
Tour1 = HanoiT([x for x in range(1, N+1)])
Tour1.affichage()
Tour2 = HanoiT([])
Tour3 = HanoiT([])
h = HanoiT()
h.movetower(N, Tour1, Tour2, Tour3)

推荐答案


引用:

但我不知道我的问题是什么问题。

but i don't know what's the problem in my case.



使用调试器查看你的代码在做什么,并在失败时检查变量。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



[]

[]

[]

[]

调试器在这里显示你的内容代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Use the debugger to see what your code is doing and inspect variables at point of failure.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于河内塔,索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:28