List sort question.

Asked 2 years ago, Updated 2 years ago, 19 views

temp=0
a=[0,0,0,0]
a[0]=str (input ("first digit : ")))
a[1]=str (input ("2nd digit: ")))
a[2]=str (input ("third digit: ")))
a[3]=str (input ("fourth digit: ")))


for i in range(0,3):
    max=i
    for j in range(0,3):
        if a[j]<a[max]:
            max=j
    temp=a[i]
    a[i]=a[max]
    a[max]=temp

for i in range(0,4):
    print(a[i], end="")

I wanted to input a number and print it out from the big value to the front, but I didn't want the numbers I didn't want I tried to apply it to Python by copying the same part of the alignment in C language It's blocked... If anyone knows, I'd like yourselves!

python

2022-09-22 21:24

1 Answers

For Python, you can use a function called sort.

a=[3,2,1]
a.sort()
print(a)

Additionally... There's a part where you're using Python like C, so let me explain In Python, the length of the list may be stabilized in advance. So when you get input,

a=[] # Declares only as a list without pre-determined size
for i in range(0,4)
    a.append(str(input("{}th digit : ").format(i+1))))

I'd like it to be typed like this.

It would be better if you print it out like this.

a=[1,2,3,4]
for n in a:
    print(n, end=" ")


2022-09-22 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.