简体   繁体   English

在python中调试代码

[英]debugging code in python

I am writing some code to do this: fill an array with numbers from 0 to 9 and then put the ones smaller than 5 at the end of it. 我正在编写一些代码来做到这一点:用0到9的数字填充数组,然后将小于5的数字放在末尾。 My code is this: 我的代码是这样的:

    print ("give n: ")
    n = int(input())
    a = []
    for i in range(n):
        num = 0
        print ("give a number from 0 to 9 ")
        num = int(input())
        while (num > 9 and num < 0):
            print ("only from 0 to 9")
            num = int(input())
        a.append(num)
    tmp = 0
    for i in range(n):
        if (a[i]<5):
            tmp = a[i]
            a[i] = a[i+1]
            a[i+1] = tmp
    for i in range(n):
        print a[i]

My problem is that when i run it, the loop that checks if a number is from 0 to 9 is ignored if i give anything else and it passes it to the array and that it gives me an error on line 16 (a[i] = a[i+1]). 我的问题是,当我运行它时,如果我给出其他任何内容,它将忽略检查数字是否为0到9的循环,并将其传递给数组,并在第16行给我一个错误(a [i] = a [i + 1])。 Thanks in advance! 提前致谢!

有适用于python的调试器: https : //docs.python.org/3/library/pdb.html可能对于一开始就学习它很有用。

For debugging in Python. 用于在Python中进行调试。 Add import pdb and then put pdb.set_trace() where you want the debugger to break. 添加import pdb ,然后将pdb.set_trace()放在调试器要中断的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM