我需要进行哪些修改才能使其不打印 none ? (使用链表)

[ad_1]

# Snake_ladder game using doubly linked list

class Empty(Exception):
    """ Error attempting to access an element from an empty container."""
    pass


class DoublyLinkedBase:
    """Queue implementation using circularly linked list for storage."""

    # -------------------------- nested Node class --------------------------
    class _Node:
        """ Lightweight, nonpublic class for storing a doubly linked node. """
        __slots__ = '_element', '_prev', '_next'  # streamline memory usage

        def __init__(self, element, prev, next):  # initialize node’s fields
            self._element = element  # reference to user’s element
            self._prev = prev  # previous node reference
            self._next = next  # next node reference

    # ------------------------------- methods -------------------------------

    def __init__(self):
        """ Create an empty list."""
        self._header = self._Node(None, None, None)
        self._trailer = self._Node(None, None, None)
        self._header._next = self._trailer  # trailer is after header
        self._trailer._prev = self._header  # header is before trailer
        self._size = 0  # number of elements

    def __len__(self):
        """Return the number of elements in the list."""
        return self._size

    def is_empty(self):
        """Return True if list is empty."""
        return self._size == 0

    def insert_between(self, e, predecessor, successor):
        """Add element e between two existing nodes and return new node."""
        newest = self._Node(e, predecessor, successor)  # linked to neighbors
        predecessor._next = newest
        successor._prev = newest
        self._size += 1
        return newest

    def delete_node(self, node):
        """ Delete nonsentinel node from the list and return its element."""
        predecessor = node._prev
        successor = node._next
        predecessor._next = successor
        successor._prev = predecessor
        self._size -= 1
        element = node._element  # record deleted element
        node._prev = node._next = node._element = None  # deprecate node
        return element  # return deleted element

    def display_board(self):
        current = self._header._next
        while current != self._trailer:
            print(current._element, end=" ")
            current = current._next
        print()

    def dice_roll_sequance(self, case):  # not a random sequance
        if case == 1:
            self._dice_seq = [1, 5, 3]  # 84
            self.move_()
            print("case %d, ans = 84" % case)
        if case == 2:
            self._dice_seq = [4, 3, 6, 1]  # 38
            self.move_()
            print("case %d, ans = 38" % case)
        if case == 3:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 5]  # 100 win
            self.move_()
            print("case %d, ans = 100 win" % case)
        if case == 4:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 1]  # 61
            self.move_()
            print("case %d, ans = 61 " % case)
        if case == 5:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 6]  # 95
            self.move_()
            print("case %d, ans = 95" % case)
        if case == 6:
            self._dice_seq = [1]  # 38
            self.move_()
            print("case %d, ans = 38" % case)
        if case == 7:
            self._dice_seq = [6, 1, 2]  # 5
            self.move_()
            print("case %d, ans = 5" % case)
        if case == 8:
            self._dice_seq = [6, 1, 2, 3]  # 31
            self.move_()
            print("case %d, ans = 31" % case)
        if case == 9:
            self._dice_seq = [1, 6, 6, 1, 6, 2]  # 99
            self.move_()
            print("case %d, ans =99" % case)

    def inistialize_board(self):
        for i in range(1, 101):

            if i == 2:
                self.insert_last(i, 36)
            elif i == 8:
                self.insert_last(i, 23)
            elif i == 17:
                self.insert_last(i, 6)
            elif i == 21:
                self.insert_last(i, 21)
            elif i == 33:
                self.insert_last(i, -28)
            elif i == 46:
                self.insert_last(i, 38)
            elif i == 51:
                self.insert_last(i, 16)
            elif i == 54:
                self.insert_last(i, -20)
            elif i == 63:
                self.insert_last(i, -47)
            elif i == 69:
                self.insert_last(i, i + 2)
            elif i == 71:
                self.insert_last(i, 20)
            elif i == 75:
                self.insert_last(i, 5)
            elif i == 80:
                self.insert_last(i, 19)
            elif i == 93:
                self.insert_last(i, -19)
            elif i == 96:
                self.insert_last(i, 1)
            elif i == 97:
                self.insert_last(i, -36)
            else:
                self.insert_last(i, 0)

    def move_(self):
      current = self._header or self._trailer
      if current is not None:
          for roll in self._dice_seq:
              for _ in range(roll):
                  if current._next is not None and current._next._element is not None and current._next != self._trailer:
                      current = current._next
                      print("Moved to square", current._element)
                  else:
                      break
          if current is not None and current != self._trailer:
              self._header = current
              print("Current position:", current._element)
          else:
              print("Current position: None")
      else:
          print("Current position: None")


    def check_win(self):
        return self._header._element == 100

    # Add this method inside the DoublyLinkedBase class

    def insert_last(self, square_id, move_shift):
        current = self._header._next
        while current:
            if current._element == square_id:
                current._element += move_shift
                # Ensure the circular behavior
                if current._element > 100:
                    current._element = 200 - current._element
                print("Moved to square", current._element)
                self._header = current  
                return
            current = current._next

     
# Main dirver

dl = DoublyLinkedBase()

dl.inistialize_board()

dl.display_board()

for i in range(1, 10):
    dl.dice_roll_sequance(i)

我尝试过的:

我需要做什么修改才能不打印任何内容?(代码链接列表)
输出 :

Current position: None
case 1, ans = 84
Current position: None
case 2, ans = 38
Current position: None
case 3, ans = 100 win
Current position: None
case 4, ans = 61 
Current position: None
case 5, ans = 95
Current position: None
case 6, ans = 38
Current position: None
case 7, ans = 5
Current position: None
case 8, ans = 31
Current position: None
case 9, ans =99

解决方案1

首先,首先找出它打印“当前位置:无”的位置 – 有两个地方明确打印该内容,并且如果评估器为 current._element 曾是 None。 您可以使用调试器单步执行代码并找出答案: pdb — Python 调试器 — Python 3.12.1 文档[^] 如果您不知道如何使用它,将会有所帮助。

然后看看为什么它会达到这个位置,如果遵循什么路径以及为什么。 是什么导致事情发展成这样呢? 哪些变量没有达到预期值? 他们要做什么? 它们应该包含什么? 它们应该设置在哪里? 为什么不是?

当您了解所有这些后,代码作者(即您)应该相当清楚问题是什么,并且您应该能够修复它。

但是让代码正常运行是作业的一个重要部分,除非您能够修复这样的简单代码,否则您不会学习如何修复复杂的代码!

[ad_2]

コメント

标题和URL已复制