这个python代码或idle有什么问题?

2024-05-20 04:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

当我创建bubble()时,它可以对我的列表进行排序,但是当我在def函数外给出while循环时,它没有对列表进行排序

“}”-是“>&燃气轮机&燃气轮机在python中是空闲的

对我来说,这个while循环在def函数中工作,但在外部不工作

}}def iftrue(a):

    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return False
    return True

}}def bubble(a):

    c=0
    while not iftrue(a):
        for i in range(len(a)-1):
            if a[i]>a[i+1]:
                a[i],a[i+1] = a[i+1],a[i]
                c+=1
    return a,c

}} bubble([1,2,3,4,5,6,5,5,4,3,2,1])

([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6], 30)

}} a

[1, 2, 3, 4, 5, 5, 5, 6, 4, 3, 2, 1]

}} while not iftrue(a):

        for i in range(len(a)-1):
            if a[i]>a[i+1]:
                a[i],a[i+1] = a[i+1],a[i]


}} a

[1, 2, 3, 4, 5, 5, 5, 6, 4, 3, 2, 1]

Tags: 函数in列表forlenreturnif排序