# Shorten This Code?

So I made a simple python program. What does it do? It takes 4 weights that the user has inputted and arranges them on a ski lift to calculate the optimum sitting position for the most balanced equilibrium.

Although the code works, I had to go at it with brute force because I’ve only just started coding python and I’m not the best at it.

Here is the code:

``````print("Ski Lift Equilibrium Calculator")
n1 = input("\nName 1: ")
n2 = input("Name 2: ")
n3 = input("Name 3: ")
n4 = input("Name 4: ")
w1=float(input("\nWeight 1: "))
w2=float(input("Weight 2: "))
w3=float(input("Weight 3: "))
w4=float(input("Weight 4: "))
d1 = abs(((w1 * 3) + w2) - (w3 + (w4 * 3)))
sc = d1
d2 = abs(((w1 * 3) + w2) - (w4 + (w3 * 3)))
if d2 < sc:
sc = d2
d3 = abs(((w1 * 3) + w3) - (w2 + (w4 * 3)))
if d3 < sc:
sc = d3
d4 = abs(((w1 * 3) + w3) - (w4 + (w2 * 3)))
if d4 < sc:
sc = d4
d5 = abs(((w1 * 3) + w4) - (w2 + (w3 * 3)))
if d5 < sc:
sc = d5
d6 = abs(((w1 * 3) + w4) - (w3 + (w2 * 3)))
if d6 < sc:
sc = d6
d7 = abs(((w2 * 3) + w1) - (w3 + (w4 * 3)))
if d7 < sc:
sc = d7
d8 = abs(((w2 * 3) + w1) - (w4 + (w3 * 3)))
if d8 < sc:
sc = d8
d9 = abs(((w2 * 3) + w3) - (w1 + (w4 * 3)))
if d9 < sc:
sc = d9
d10 = abs(((w2 * 3) + w3) - (w4 + (w1 * 3)))
if d10 < sc:
sc = d10
d11 = abs(((w2 * 3) + w4) - (w1 + (w3 * 3)))
if d11 < sc:
sc = d11
d12 = abs(((w2 * 3) + w4) - (w3 + (w1 * 3)))
if d12 < sc:
sc = d12

print("\nThe best solutions is:")
if sc is d1:
print(str(n1) + " " + str(n2) + " " + str(n3) + " " + str(n4))
if sc is d2:
print(str(n1) + " " + str(n2) + " " + str(n4) + " " + str(n3))
if sc is d3:
print(str(n1) + " " + str(n3) + " " + str(n2) + " " + str(n4))
if sc is d4:
print(str(n1) + " " + str(n3) + " " + str(n4) + " " + str(n2))
if sc is d5:
print(str(n1) + " " + str(n4) + " " + str(n2) + " " + str(n3))
if sc is d6:
print(str(n1) + " " + str(n4) + " " + str(n3) + " " + str(n2))
if sc is d7:
print(str(n2) + " " + str(n1) + " " + str(n3) + " " + str(n4))
if sc is d8:
print(str(n2) + " " + str(n1) + " " + str(n4) + " " + str(n3))
if sc is d9:
print(str(n2) + " " + str(n3) + " " + str(n1) + " " + str(n4))
if sc is d10:
print(str(n2) + " " + str(n3) + " " + str(n4) + " " + str(n1))
if sc is d11:
print(str(n2) + " " + str(n4) + " " + str(n1) + " " + str(n3))
if sc is d12:
print(str(n2) + " " + str(n4) + " " + str(n3) + " " + str(n1))
print("The momental force difference is: " + str(sc))
print("\nPleas note: There may be other solutions with the same m force difference.")

wait = input("\n \n \nPress enter to exit")
``````

If anyone could sugget ways to shorten it, that would be great. Sorry if my bad coding makes you want to tear your eyes out :o

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.