1

I was wondering what type I would have to call when my program has to know that my user input is an object name.

Example:

If I have this object in a class

Jens = Person("Jens", "14 years")

search = input("What person do you want to print info about? ") 

search.printInfo() 

What type should I wrap around search to make it possible to set it into my printInfo function?

Thanks in advance

I am working in python3

SnitchingAuggie
  • 494
  • 3
  • 14
  • 2
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Aran-Fey Apr 19 '18 at 21:41
  • 2
    Put your objects into a dictionary and use the name as a key. If you want to retrieve it by more than one attribute, put it in multiple dictionaries. Or else use a database. – kindall Apr 19 '18 at 21:42
  • I am trying to create a database and I have a dictionary. I just can't print one single key which is what I want – SnitchingAuggie Apr 19 '18 at 21:44
  • Not a duplicate at all. I want to know what type the object is thx – SnitchingAuggie Apr 19 '18 at 21:47
  • Say you have `x = 5`. The name `x` isn't a python object. You can access the mappings of names to objects using the `globals` and `locals`. functions, but it makes much more sense to simply have a separate dictionary that you own and can pass around between functions. – Patrick Haugh Apr 19 '18 at 22:27
  • If I have a dictionary then and I want to print just one of the keys instead of all. – SnitchingAuggie Apr 20 '18 at 06:04
  • How do i search just one single key and print that particular key? – SnitchingAuggie Apr 20 '18 at 06:46
  • You don't have to search at all; it's just a key in the dictionary, so you can access it directly. `my_dict[my_key]` – kindall Apr 20 '18 at 23:11
  • I see but I can't access one specific key. If I have dict = {Gertrud: 5, James: 4, Bente: 5} How do I print only James? Without printing the others – SnitchingAuggie Apr 21 '18 at 06:45

0 Answers0