I have a question about how to call the variable name created through Python operation.

Asked 2 years ago, Updated 2 years ago, 17 views

I would like to ask you how you can call the variable name obtained through the calculation result.

For example,

year_list = [2020, 2021, 2022]
for i in year_list :  
   name = str(i) + 'data'
   print(name)

<Result value>

2020data 
2021data
2022data 

--- Description (question) ---

You want to create a dictionary with the result value obtained through the for statement above as a variable name. (Question 1 part)

ex) 2020data = {}, 2021data= {}, 2022data= {}

And when you run another operation in the future, you want to call up the variable name you want (ex) 2020data) and proceed with another operation.

Here's the question!

python

2022-09-20 10:53

1 Answers

data_holder = dict()
while True:
    k = input ("data name:")
    if k == "end":
        break
    data = input ("data to store:")
    data_holder[k] = data
while True:
    k = input ("What data do you want to see?"")
    if not k in data_holder:
        No data is saved under the name print(f"{k}.")
        continue
    print(f"data {k} is {data_holder[k]}.")


2022-09-20 10:53

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.