Other people explained the problem solving using the sort function, but if you use the for statement, I was wondering what I could do, and I was wondering if there's a way to reduce the sauce Here's the question.
a=[0,0,0,0]
sum=0
max=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,4):
for j in range(0,4):
for k in range(0,4):
for l in range(0,4):
if i!=j:
if i!=k:
if i!=l:
if j!=k:
if j!=l:
if k!=l:
sum=a[i]+a[j]+a[k]+a[l]
if max<int(sum):
max=int(sum)
else:
continue
else:
continue
else:
continue
else:
continue
else:
continue
else:
continue
else:
continue
print ("The largest number is %d." %max)
We reduced the depth of the for statement and reduced the number of entries.
But I don't know why you want to put a SORT and make a For Moon.
a=input ("Please enter 4 numbers:")split()
sum, max = 0, 0
for i in range(0,4):
for j in range(0,4):
for k in range(0,4):
for l in range(0,4):
if i!=j and i!=k and i!=l and j!=k and j!=l and k!=l:
sum=a[i]+a[j]+a[k]+a[l]
if max<int(sum):
max=int(sum)
else:
continue
print ("The largest number is %d." %max)
© 2024 OneMinuteCode. All rights reserved.