Q:

How to compare two objects in a dictionary

I just wonder that how to compare two objects in a dictionary:
a = {}
while z:
    if a[z] == a[s]:
        print("Correct!")
    else:
        print("You don't know that country.")
    z = input("Enter a country: ")
    s = input("What is the capital of " + z + " ? ")

I want to print Correct when z = s; when z not in a, print You don't know that country.

A:

Assuming that a is a dictionary with the country as the key, and the capital as the value, you should make two separate checks: First you want to make sure that the entered country exists as a key in the dictionary. For that you should use the in operator. Second, you want to check whether the entered capital matches the capital in the dictionary. For that you perform an index access using the country on the dictionary and check whether the value equals the entered capital:
capitals = {
    'Austria': 'Vienna',
    'Belgium': 'Brussels',
    'Denmark': 'Copenhagen',
    'France': 'Paris',
    'Germany': 'Berlin',
    'Netherlands': 'Amsterdam',
    'Norway': 'Oslo',
    'Sweden': 'Stockholm',
    'Switzerland': 'Bern',
    'United Kingdom': 'London'
}

while True:
    country = input("Enter a country: ")

    # abort the loop if the user didn’t enter anything
    if not country:
        break

    # check whether we know that country
    if country in capitals:
        capital = input("What is the capital of {}? ".format(country))

        if capital == capitals[country]:
            print('That was correct!')
        else:
            print('You made a mistake there.')

    else:
        print('I do not know that country, sorry.')

