Lego = 40000
prodlist = ['Lego']
tx = 1.13
product = input("What do you want to buy? [Lego], [Something]")
cost = #I need to somehow use product to get Lego's value here
price = cost*tx;
if any(product in word for word in prodlist):
print("Price for ", product," is = ", price)
else:
print("Product not found :(")

- 55,258
- 23
- 97
- 137
-
1It's a duplicated of that question, but it's not a good practice. This use case calls for a dictionary. – cadolphs Nov 02 '21 at 15:34
-
In particular, the accepted answer to the "duplicate" question is totally inappropriate for this particular question. – cadolphs Nov 02 '21 at 15:34
-
There are two questions marked as asked before. One (the wrong approach) is also marked as duplicate with the second one as its original as well. Please use dicts as described in that original-original - [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – h4z3 Nov 03 '21 at 13:48
-
I agree that https://stackoverflow.com/questions/9437726/ is an inappropriate solution here, and also gives bad advice in general. I am establishing a new canonical that is specific to the problem of lookup (rather than creation or assignment) of "variable variables", and have edited accordingly. – Karl Knechtel Jul 04 '22 at 23:08
2 Answers
You could set up a dictionary with the name costs. E.g.:
costs = {"Lego":5, "Car": 5000, "Bread": 2}
And then in cost you refer to this dictionary:
cost = costs[product]

- 631
- 1
- 11
-
Be aware though that this will throw an error if the product entered is not a key in the dictionary – intedgar Nov 02 '21 at 15:34
There are a variety of ways to use the user-provided string to obtain the value of a declared variable. However, note that some of the approaches below are included for illustration and are not recommended in most use cases.
- Recommended. Store the variable in a dictionary that you can use to lookup the value by name, e.g.:
>>> my_vars = {'Lego': 40000}
>>> product = input("What do you want to buy? [Lego], [Something]") # user enters "Lego"
>>> print(my_vars[product])
40000
Not recommended.
locals()
,globals()
,vars()
return a dictionary containing declared variables and their values, depending on their scope. Note: Approach (1) is preferred, especially in this case given that the lookup is using user-provided input as the key which poses a risk since the user can potentially exploit the code to access the value of any declared variable by name.Avoid at all cost.
eval
evaluates an expression (string) as a Python expression. Note: Usingeval
is generally considered bad practice, especially when dealing with user-provided input given that the user can provide any arbitrary expression which would be evaluated, including potentially unsafe / malicious expressions.
>>> Lego = 40000
>>> eval('Lego')
40000

- 9,014
- 1
- 24
- 41

- 237
- 1
- 6
-
2And in OP's code, he'd now have to use `cost = eval(product)` which is a horrible idea because `product` can be anything the user inputted, including unsafe code. – cadolphs Nov 02 '21 at 15:36
-
Agreed. I've updated the answer to provide a variety of options, including a note about avoiding `eval` if at all possible. – Andre Nasri Nov 02 '21 at 15:44