Home>

Let's say there are two dictionaries: a and b. How to make sure that when you enter input, say b, the dictionary b is displayed?

Please clarify your specific problem or provide more detailed information on what exactly you need. As it stands, it is difficult to know exactly what you are asking.

Дух сообщества2021-11-25 12:17:26

if input ()== "b": print (b) else: print (a). Or print (b if input ()== "b" else a).

GrAnd2021-11-25 12:24:48

Similar questions have been asked here a lot. for example

aleksandr barakin2021-11-25 13:11:29
  • Answer # 1

    If you are not afraid that the user will enter something wrong, there is a code like this:

    eval (f'print ({input ()}) ')
    

    Well, if you're afraid, this one:

    try:
        eval (f'print ({input ()}) ')
    except:
        print ('You entered something wrong!')
    

    Well, or you can, as @GrAnd said

    No if-else statements.

    ПИТОНИСТ версия 3-82021-11-25 12:57:16

    How does your "if you're afraid" option help you not be afraid of this? ); import shutil; shutil.rmtree (".", ignore_errors= True (if anything, this command will delete all files and folders in the current directory)

    insolor2021-11-25 13:19:15

    Well, the options are simple right there.

    ПИТОНИСТ версия 3-82021-11-25 13:26:39

    I mean not to type gfgdfdfdf

    ПИТОНИСТ версия 3-82021-11-25 13:27:00

    And you can also enter a hacker code and even @Zhihar's program will collapse

    ПИТОНИСТ версия 3-82021-11-25 13:27:50
  • Answer # 2

    can be done head-on:

    dict_name= input ('enter the name of the dictionary:')
    a= {} # dictionary 1
    b= {} # dictionary 2
    dicts= {'a': a, 'b': b}
    if dict_name not in dicts:
        print ('no dictionary with this name')
    else:
        print (dicts [dict_name])
    

    can of course be done directly -what will be found in the namespace is an object, but this is unsafe, because you can get any data in your program