user2757442
user2757442

Reputation: 175

Dictionary values are not showing up in a later section of code when they are printed

Ignore all of this cod except the spots that I point out, I will say ##Look Here## at the spots:

ans = raw_input('Enter Amount of Players: ').lower()

if ans == '2':
    a = raw_input('What is Player 1 named: ')
    b = raw_input('What is Player 2 named: ')
    cf={a:{}, b:{}}
    cf[a] = a
    cf[b] = b
    p1 = raw_input(a + ", what is your city named: ")  
    p2 = raw_input(b + ", what is your city named: ")
    cf={a:{p1:50}, b:{p2:50}}
    ##Look here, print cf ##
    print cf
    for key, cf in cf.items():
        print(key)
        for attribute, value in cf.items():
            print('{} : {}'.format(attribute, value))

answer = raw_input ("Hit 'Enter' to continue")

def cva(x):
    y = cf[ques][city]
    y = float(y)
    return x + y

while True:
    one = raw_input("Type 'view' to view civil form, type 'change' to change civil order, or 'add' to add a city: ")
    if one == 'change':
        ques = raw_input('Enter Name of Player That Will Change His/Her Civil Form: ').lower()
        city = raw_input(ques + ' Enter Name Of City That Will Change Civil Form: ').lower()
        inc = raw_input(ques + ' Enter Amount of change for ' + city + ": ").lower()
        ##Look here, print cf##
        print cf
        cf[ques][city]=cva(float(inc))
        for key, cf in cf.items():
            print(key)
            for attribute, value in cf.items():
                print('{} : {}'.format(attribute, value))
    elif one == 'view':
        for key, cf in cf.items():
            print(key)
            for attribute, value in cf.items():
                print('{} : {}'.format(attribute, value))
    elif one == 'add':
        ch1 = raw_input('Enter the Name of Player That Will Add a City: ')
        ch2 = raw_input(ch1 + ', Enter The Name of Your New City: ')
        cf[ch1][ch2] = '50'
    elif one == 'over': break
    elif one == 'game over': break
    elif one == 'quit': break

The 2 parts of the code that I told you to look at basically print the dictionary. When I input the name 'Andrew' and 'Matt', and the cities 'Po' and 'Lo' the first print looks like this:{'Matt': {'Lo': 50}, 'Andrew': {'Po': 50}} The second print looks like this for Matt: {'Po': 50} The second print looks like this for Andrew: {'Lo': 50} Why does the name of the player not show up in the second print like it did in the first print. Is it because the name got deleted? If so, can you tell me how to fix this problem?

Upvotes: 0

Views: 67

Answers (1)

Hannes Ovrén
Hannes Ovrén

Reputation: 21831

The problem is your loop

for key, cf in cf.items():
        for attribute, value in cf.items():
            print('{} : {}'.format(attribute, value))

Since you name one of the iteration variables to the same thing as the dictionary you are iterating over, you have problems. cf will, after this loop, contain the value of the last item.

Rename the iteration variable cf to something else, like

for key, dict_val in cf.items():
            for attribute, value in dict_val.items():
                print('{} : {}'.format(attribute, value))

and things should be fine.

Upvotes: 1

Related Questions