Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use input() to specify a dictionary in Python

Python – I’m beginner. I want to use input() to call one of possible dictionaries (in my example there are: NN and NN1) and then run functions on that chosen dictionary.
Here is part of my code (I need this "tik = i" later, but now it does not matter):

NN = {"short name1": "full name1", "short name2": "full name2", "short name3": "full name3"}

NN2 = {"short name4": "full name4", "short name5": "full name5", "short name6": "full name6"}

dict1 = input ("your choice: NN / NN1? ")

for i, j in dict1.items(): 

    tik = i 

    print(j)

When I run it, there is:

"for i, j in dict1.items():
AttributeError: ‘str’ object has no attribute ‘items’ "

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it possible to use input function or do I need sth?

>Solution :

you need these changes in your code:

inp_dict = {'NN':NN , 'NN1':NN2}
dict1 = input ("your choice: NN / NN1? ")
for i, j in inp_dict[dict1].items():

    tik = i

    print(j)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading