0

I have a few variables with different names and values

var_1= 10, var_2 = 20  , var_4 = 40 , var_5 = 50

and I have a variable called i that has a value assigned in this case i = 4

how can select the variable with a wild card based on the value of i?

like in some programing languages like java it would be something like this

select_value = var_+i the result of printing selected_value would be 40.

Thanks

jlozasi
  • 57
  • 4
  • 1
    Nothing like that would work in Java. I don't think any sane programming language would allow it either. What you need is a `list`, not multiple variables. – Selcuk Jan 13 '21 at 05:02
  • Absolutely that would not work in Java. In any case, you **definitely shouldn't be designing your program this way**. Instead of dynamically using variables like this, use a *container* like a list or a dict. – juanpa.arrivillaga Jan 13 '21 at 05:32
  • i guess u are asking to do something like variable1 = variable1 + i and after passing several values u get different values of variable1. Well to do so u are asking for iterations and u can use any loop like while /for /if loop. and u don't need to specify particularly for i in python and u need to properly write for using various variables in a loop Hope it helped. – Rupesh Kumar Jan 13 '21 at 05:07

2 Answers2

0

At that point don't use variables and instead store them in a dictionary:

var = {
    'var_1': 10,
    'var_2': 20,
    'var_4': 40,
    'var_5': 50
}

print(var['var_4'])
>> 40
drum
  • 5,416
  • 7
  • 57
  • 91
0

I'm sure there are some un-pythonic or security issues with this method, but it gets the job done:

eval(f"var_{i}")
goalie1998
  • 1,427
  • 1
  • 9
  • 16
  • 1
    Only a maniac would use this in a serious project lol – Mick Jan 13 '21 at 05:04
  • Like I said, I'm sure there are issues, but it does what OP wants – goalie1998 Jan 13 '21 at 05:04
  • 1
    Please don't suggest bad programming practices. The OP has an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) which should be solved in a totally different way. – Selcuk Jan 13 '21 at 05:06
  • It's a good answer according to the OP's question – Mick Jan 13 '21 at 05:07
  • @Selcuk, can you explain what OP's actual problem is? – goalie1998 Jan 13 '21 at 05:09
  • 1
    Their actual problem (X) is to have a variable number of values, and to be able to access them in an arbitrary order. Their solution (Y) to this is to create multiple variables, which is obviously an inferior method. Solving Y does not make sense, so we should focus on solving X instead. – Selcuk Jan 13 '21 at 05:12
  • Or, it's also possible that problem (X) is that OP has, fully outside his control, a group of variables named as such that would cause a second problem if OP wanted to say add them to a list in a particular order, or a dictionary, every time a new variable was delivered – goalie1998 Jan 13 '21 at 05:14
  • 1
    That's also possible, although highly unlikely. I guess the OP is not interested in clarifying it either. In any case, blindly [suggesting `eval`](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) does not help anyone. For example `globals().get(f"var_{i}")` would be a safer (although equally bad) solution. – Selcuk Jan 13 '21 at 05:15