Reputation: 25
This is the code for a calculator I made with Turtle, the list contains the different digits and operators to be used. The If statement checks the lenght of the list, and for every +1 element added to the list, it moves the coordinates of the turtle pen which will then write the numbers that were input by the user in a calculator "display".
How can a I make this code more efficient?
def order_display():
if len(list) == 1:
turtle.goto(245, 110)
elif len(list) == 2:
turtle.goto(230, 110)
elif len(list) == 3:
turtle.goto(215, 110)
elif len(list) == 4:
turtle.goto(200, 110)
elif len(list) == 5:
turtle.goto(185, 110)
elif len(list) == 6:
turtle.goto(170, 110)
elif len(list) == 7:
turtle.goto(155, 110)
elif len(list) == 8:
turtle.goto(140, 110)
elif len(list) == 9:
turtle.goto(125, 110)
elif len(list) == 10:
turtle.goto(110, 110)
elif len(list) == 11:
turtle.goto(95, 110)
elif len(list) == 12:
turtle.goto(80, 110)
elif len(list) == 13:
turtle.goto(65, 110)
elif len(list) == 14:
turtle.goto(50, 110)
elif len(list) == 15:
turtle.goto(35, 110)
elif len(list) == 16:
turtle.goto(20, 110)
elif len(list) == 17:
turtle.goto(5, 110)
elif len(list) == 18:
turtle.goto(-10, 110)
else:
print("DOES NOTHING")
Upvotes: 0
Views: 80
Reputation: 1166
Use a for loop :
if 1 <= len(list) <= 18:
for i in range(1, 18):
if len(list) == i:
turtle.goto(-15 * i + 260, 110)
else: print("DOES NOTHING")
Upvotes: 1