Program simplification

Hi, I need to simplify this but I don’t know how to do it. Can u help me pls

if a == "a1":
    a = a1
elif a == "a2":
    a = a2
elif a == "a3":
    a = a3
elif a == "b1":
    a = b1
elif a == "b2":
    a = b2
elif a == "b3":
    a = b3
elif a == "c1":
    a = c1
elif a == "c2":
    a = c2
elif a == "c3":
    a = c3

It looks like you’re using a bunch of different variables to hold related information. One called a1, one called a2, etc.

Don’t use separate variable. Instead use a collection (like a dictionary). Rather than separate variables, you can put the information in one variable. Then you can get the data you want by indexing into that collection.

Can you show the part where a1, a2, a3, etc. are set?

yes of course, these are coordinates for a graphic interface a visual interface

a1 = (-100, 100)
a2 = (0, 100)
a3 = (100, 100)
b1 = (-100, 0)
b2 = (0, 0)
b3 = (100, 0)
c1 = (-100, -100)
c2 = (0, -100)
c3 = (100, -100)

Do you want to write a graphical interface ?!
You would not need to re-invent an interface …
do you know “Python Turtle” ?! - it is a library !
https://docs.python.org/3/library/turtle.html
Greetz.

I’m going to use turtle’s, I must have mistranslated what I meant sorry

So rather than make them separate variables, put them in a dict or a list. If they have names, use a dict.

points = {
    "a1": (-100, 100),
    "a2": (0, 100),
    "a3": (100, 100),
    "b1": (-100, 0),
    "b2": (0, 0),
    "b3": (100, 0),
    "c1": (-100, -100),
    "c2": (0, -100),
    "c3": (100, -100),
}

a = points["c2"] # sets a to the point with the name "c2"

thank you very much this is what I did after you recommended me to use a dictionary but I wrote it like this? Is it more correct to write it like you did? It’s like if you don’t skip a space after a comma it works but it’s not correct?

coordinates = {"a1" : (-100, 100), "a2" : (-100, 100), "a3" : (100, 100), "b1" : (-100, 0), "b2" : (0, 0), "b3" : (100, 0), "c1" : (-100, -100), "c2" : (0, -100), "c3" : (100, -100)}

Both forms should work. For long lists, it’s usually easier to see it broken on multiple lines.

Did you get an error or a problem? I’m not sure what you mean by skipping a space after a comma. There are preferred styles, but the space after a comma isn’t enforced by the interpreter.

>>> dict1 = {"a":1,"b":2}
>>> dict2 = {"a" : 1, "b" : 2}
>>> dict3 = {
... "a": 1,
... "b": 2,
... }
>>> dict1 == dict2 == dict3
True

thank you very much, what I meant with my story of comma and line break is that for example these programs do exactly the same thing but the first is more correct.
first :

name = input("what's your name?\n")
age = int(input('how old are you?\n'))
print('Your name is', name, 'and you are', age, 'years old')

second :

name=input("what's your name?\n")
age=int(input('how old are you?\n'))
print('Your name is',name,'and you are',age, 'years old')

Rather than “correct”, I would say the first is preferred. But with f-strings I would replace them both with

print(f'Your name is {name} and you are {age} years old')

The python style guide is a good place to start for many style questions, but it does not seem to mention spaces after commas in a list.