2

I have only been working with python for a few months, so sorry if I am asking a stupid question. I am having a problem calling a dictionary name using a variable.

The problem is, if I use a variable to call a dictionary & [] operators, python interprets my code trying to return a single character in the string instead of anything within the dictionary list.

To illustrate by an example ... let's say I have a dictionary list like below.


USA={'Capital':'Washington',
     'Currency':'USD'}

Japan={'Capital':'Tokyo',
       'Currency':'JPY'}

China={'Capital':'Beijing',
      'Currency':'RMB'}

country=input("Enter USA or JAPAN or China? ")

print(USA["Capital"]+USA["Currency"])  #No problem -> WashingtonUSD
print(Japan["Capital"]+Japan["Currency"])  #No problem -> TokyoJPY
print(China["Capital"]+China["Currency"])  #No problem -> BeijingRMB
print(country["Capital"]+country["Currency"])  #Error -> TypeError: string indices must be integers

In the example above, I understand the interpreter is expecting an integer because it views the value of "country" as a string instead of dictionary... like if I put country[2] using Japan as input (for example), it will return the character "p". But clearly that is not what my intent is.

Is there a way I can work around this?

2 Answers 2

2

You should put your countries themselves into a dictionary, with the keys being the country names. Then you would be able to do COUNTRIES[country]["Capital"], etc.

Example:

COUNTRIES = dict(
    USA={'Capital':'Washington',
         'Currency':'USD'},
    Japan={'Capital':'Tokyo',
           'Currency':'JPY'},
    ...
)
country = input("Enter USA or Japan or China? ")
print(COUNTRIES[country]["Capital"])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Tom, this is very helpful!! I don't think I would have ever considered using another dictionary to wrap the initial dictionary without your help. Again, thanks!
1

Disclaimer: Any other way of doing it is definitely better than the way I'm about to show. This way will work, but it is not pythonic. I'm offering it for entertainment purposes, and to show that Python is cool.

USA={'Capital':'Washington',
     'Currency':'USD'}

Japan={'Capital':'Tokyo',
       'Currency':'JPY'}

China={'Capital':'Beijing',
      'Currency':'RMB'}

country=input("Enter USA or Japan or China? ")

print(USA["Capital"]+USA["Currency"])  #No problem -> WashingtonUSD
print(Japan["Capital"]+Japan["Currency"])  #No problem -> TokyoJPY
print(China["Capital"]+China["Currency"])  #No problem -> BeijingRMB

# This works, but it is probably unwise to use it.
print(vars()[country]["Capital"] + vars()[country]['Currency'])

This works because the built-in function vars, when given no arguments, returns a dict of variables (and other stuff) in the current namespace. Each variable name, as a string, becomes a key in the dict.

But @tom's suggestion is actually a much better one.

1 Comment

Hi Cody, thank you also for providing an example. I've tried your method also and it also did provide the expected results... I didn't realize you can provide a 'hack-like' method using the built-in vars() function. I think such method can be useful in particular occasions if used properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.