-1
if p11 == "+":
    loc = 11
elif p12 == "+":
    loc = 12
elif p13 == "+":
    loc = 13
elif p14 == "+":
    loc = 14
elif p15 == "+":
    loc = 15

I need to have the above going from 11 until 55, so it would amazing if i could simplify it. Whatever number comes after the "p" in the variables is what loc is equal to.

And if it is possible can someone explain the reverse?

w1ndnd
  • 45
  • 5
  • 9
    You shouldn't have numbered variables in the first place - a list would make this far simpler. – user2357112 Mar 31 '23 at 21:10
  • 3
    You could do `loc = [p11, p12, p13, p14, p15].index('+') + 11`, but you should really use a `list` instead. – Unmitigated Mar 31 '23 at 21:11
  • 6
    This looks like a [xy problem](https://xyproblem.info/). Please explain what you are really trying to achieve. There might be another ways to do it. – Metin Usta Mar 31 '23 at 21:12
  • 1
    Related: [How do I create variable variables?](/q/1373164/4518341) This question may turn out to be a duplicate of it, or one of the questions linked from there. – wjandrea Mar 31 '23 at 21:14
  • @MetinUsta I am trying to have a game with a grid, the first number in, e.g. 13, is the x-pos, and the second is the y-pos. Instead of having two variables i merged it into one. The plus represents where the character is on the grid, i use the variables starting with p in the grid as {p13}. The variable is either `"+"` or `" "`. I am using the loc variable to actually tell where I am instead of every time i have to try to move check every variable if it is a plus or not. – w1ndnd Mar 31 '23 at 21:16
  • 3
    Any time you have a bunch of variables with numeric names like that, you should probably be using a list. Then you can loop over the list instead of repeating code for each variable. – Barmar Mar 31 '23 at 21:17
  • 4
    And if it's a grid, you should have a list of lists, not variables like `pXY`. – Barmar Mar 31 '23 at 21:18
  • @Unmitigated Um, that is a list. I suppose you mean, make it a list in the first place, like for example: `p = ['', '', ..., '+', ...]; loc = p.index('+') + 11` – wjandrea Mar 31 '23 at 21:18
  • @Barmar how would I reference the list in the code? Because I know how to do a variable `f"{p13}"` but not an item from a list. – w1ndnd Mar 31 '23 at 21:19
  • 2
    You use list indexing: `f"{p[1][3]}"` – Barmar Mar 31 '23 at 21:21
  • 1
    @w1ndnd Have you not learned how to use lists already? You should do a Python tutorial then. Stack Overflow isn't built to teach the basics. – wjandrea Mar 31 '23 at 21:21
  • @wjandrea Right, I should have phrased that better. – Unmitigated Apr 01 '23 at 04:07

1 Answers1

0

I don't know why you would like something like this, but in any case, I think you are looking something like this:

# The input variable
p11 = '+'

# Calculation of output (loc)
for variable_name, variable_value in locals().items():
    # Checking the variable name and the variable value
    if variable_value == '+' and len(variable_name) == 3 and variable_name.startswith('p') and \
            variable_name[1:3].isdigit() and 11 <= int(variable_name[1:3]) <= 55:
        loc = variable_name[1:3]
        print(loc)
        break
else:
    print('no results!')
Asi
  • 170
  • 2
  • 10