为什么一共十几行的代码会在几千行的地方报错?(语言-python)

img

同样的代码在我朋友的电脑上却能正常运行,这是为什么,一些需要的库也已经引入了

错误在34行(看你的最下面的提示,那个文件才是你自己编写的)
上面那些,都是你的代码又调用了系统库,在系统库里产生错误的行,和你关系不大。

代码贴出来看看

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7740724
  • 这篇博客也不错, 你可以看下爬虫项目二十一:需要简历吗?用Python轻松爬下上千份简历模板
  • 除此之外, 这篇博客: 方法解析:判断给定序列对应的二叉搜索树是否相同(python实现)中的 代码实现 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    # Judge the same binary search tree
    # Edited by Ocean_Waver
    
    # 由于二叉搜索树不同于二叉树,仅根据二叉搜索树的先序遍历结果就能判定二叉树的唯一形状;
    # 因此我们可以通过比较两个二叉搜索树的先序遍历结果来判断他们是否为同一个二叉搜索树;
    
    
    class node:
        def __init__(self, elem=None, lchild=None, rchild=None):
            self.elem = elem
            self.lchild = lchild
            self.rchild = rchild
    
    
    class tree:
        def __init__(self, root=node()):
            self.root = root
    
    
    def bi_search_tree_establish(List):		# 根据输入的列表建立二叉搜索树
        if List:
            mytree = tree(node(List[0]))
            for i in range(1, len(List)):
                temp_node = mytree.root
                while temp_node:
                    if List[i] < temp_node.elem:
                        if temp_node.lchild:
                            temp_node = temp_node.lchild
                        else:
                            temp_node.lchild = node(List[i])
                            break
                    else:
                        if temp_node.rchild:
                            temp_node = temp_node.rchild
                        else:
                            temp_node.rchild = node(List[i])
                            break
            return mytree
        else:
            return None
    
    
    def preorder_probing(now_node, pre_L):  # 先序遍历——递归实现
        # print(now_node.elem)
        pre_L.append(now_node.elem)
        if now_node.lchild:
            preorder_probing(now_node.lchild, pre_L)
        if now_node.rchild:
            preorder_probing(now_node.rchild, pre_L)
    
    
    def cmp(a, b):		# 比较序列函数,Python3的常用函数库里已经没有cmp函数了
        leng = len(a)
        if leng == len(b):
            for i in range(0, leng):
                if a[i] != b[i]:
                    # print("False")
                    return -1
            # print("True")
            return 0
        else:
            # print("False")
            return -1
    
    
    if __name__ == "__main__":
    
        N = int(input())		# 输入n表示需要检测的组数
        S_List = [int(i) for i in input()]		# 输入一个二叉搜索树序列作为标准,与下面的进行比较
        S_Tree = bi_search_tree_establish(S_List)		# 构建标准二叉搜索树
    
        if S_Tree:
            S_pre_list = []
            preorder_probing(S_Tree.root, S_pre_list)
    
            for i in range(0, N):
                List = [int(i) for i in input()]		# 输入待比较的二叉搜索树序列
                MyTree = bi_search_tree_establish(List)		# 构建待比较二叉搜索树
                if MyTree:
                    pre_list = []
                    preorder_probing(MyTree.root, pre_list)
    
                    if cmp(S_pre_list, pre_list) == 0:
                        print("YES")
                    else:
                        print("NO")
    
    
  • 您还可以看一下 CSDN就业班老师的第二章 程序设计与数据结构课程中的 Python的魔法功能,魅力之所在,让代码量更少 2小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^