我不确定这个python程序有什么问题

2024-03-29 11:53:11 发布

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

我正在编写一个冒泡排序算法。在我尝试添加第二部分之前,它一直工作得很好,第二部分实质上是将输入(“c”)更改为一个float,只有在需要的时候

def bs(l):
    s=True
    while s:
        s=False
        for i in range(0,len(l)-1):
            if l[i]>l[i+1]:
                s=True
                h=l[i+1]
                l[i+1]=l[i]
                l[i]=h
    return l

b=[]
for q in range(int(input("How many numbers do you want to sort?"))):
    print("Enter the value for place number",q+1)
    c=input()
    c=list(c)
    if "." in c:
        c="".join(c)
        c=float(c)
        b.append(c)
    c=int(c)
    b.append(c)

print("\nThe numbers in order, are:\n\nSmallest",bs(b),"Largest\n")

谢谢你,任何帮助都将不胜感激


Tags: in算法trueforinputifbsdef
1条回答
网友
1楼 · 发布于 2024-03-29 11:53:11

您应该更改此部分:

if "." in c:
    c="".join(c)
    c=float(c)
    b.append(c)
c=int(c)
b.append(c)

有别的:分支

if "." in c:
    c="".join(c)
    c=float(c)
    b.append(c)
else:
    c=int(c)
    b.append(c)

相关问题 更多 >