Python Function - Looping through dictionary and updating value

Viewed 75

I'm having an issue wherein i'm trying to loop through a dictionary to update all the values based on a specific input from a user.

I've posted my code below, basically, the user inputs a number into the different lanes played input boxes, and the function should update the dictionary to contain the values input by the user.

the loop doesn't seem to be going through every key in my dictionary, only returning the first value and then seemingly skipping the rest.

I am only 3 weeks deep into a python tutorial and I'm not really sure what i am doing wrong here.

please see code below :

def lane_input():

lanes_played = {"Top":0, "Middle":0, "Jungle":0, "Support":0, "ADC":0}

top_played = input("insert the number of times top played")
top_played = int(top_played) 

mid_played = input("insert the number of times Middle played")
mid_played = int(mid_played)

Jungle_played = input("insert the number of times Jungle played")
Jungle_played = int(Jungle_played)

Support_played = input("insert the number of times Support played")
Support_played = int(Support_played)

ADC_played = input("insert the number of times ADC played")
ADC_played = int(ADC_played)

for p, n in lanes_played.items():
    if p == 'Top': 
        lanes_played[p] = top_played
        
    if p == 'Middle': 
        lanes_played[p] = mid_played
    
    if p == 'Jungle': 
        lanes_played[p] = Jungle_played
    
    if p == 'Support': 
        lanes_played[p] = Support_played
        
    if p == 'ADC': 
        lanes_played[p] = ADC_played
    
    return(lanes_played)
        
print(lanes_played)
print(lanes_played[p])
print(lanes_played[n])

enter code here`enter code here`total = lane_input()
enter code here`print(total)

Code output below :

insert the number of times top played1
insert the number of times Middle played2
insert the number of times Jungle played3
insert the number of times Support played4
insert the number of times ADC played5

{'Top': 1, 'Middle': 0, 'Jungle': 0, 'Support': 0, 'ADC': 0}
3 Answers

It's because of bad indentation in your code. The return keyword is hit at the first iteration of the for loop, which makes your function stop.

This works, since return happens after the whole for loop:

def lane_input():

        lanes_played = {"Top":0, "Middle":0, "Jungle":0, "Support":0, "ADC":0}

        top_played = input("insert the number of times top played")
        top_played = int(top_played) 

        mid_played = input("insert the number of times Middle played")
        mid_played = int(mid_played)

        Jungle_played = input("insert the number of times Jungle played")
        Jungle_played = int(Jungle_played)

        Support_played = input("insert the number of times Support played")
        Support_played = int(Support_played)

        ADC_played = input("insert the number of times ADC played")
        ADC_played = int(ADC_played)

        for p, n in lanes_played.items():
                if p == 'Top': 
                        lanes_played[p] = top_played
                        
                if p == 'Middle': 
                        lanes_played[p] = mid_played
                
                if p == 'Jungle': 
                        lanes_played[p] = Jungle_played
                
                if p == 'Support': 
                        lanes_played[p] = Support_played
                        
                if p == 'ADC': 
                        lanes_played[p] = ADC_played
        
        return(lanes_played)

The return is inside the for-loop, thus the iteration stops after the first round only. This is what you meant to do:

def lane_input():
    lanes_played = {"Top":0, "Middle":0, "Jungle":0, "Support":0, "ADC":0}

    top_played = input("insert the number of times top played")
    top_played = int(top_played) 

    mid_played = input("insert the number of times Middle played")
    mid_played = int(mid_played)

    Jungle_played = input("insert the number of times Jungle played")
    Jungle_played = int(Jungle_played)

    Support_played = input("insert the number of times Support played")
    Support_played = int(Support_played)

    ADC_played = input("insert the number of times ADC played")
    ADC_played = int(ADC_played)

    for p, n in lanes_played.items():
        if p == 'Top': 
            lanes_played[p] = top_played

        if p == 'Middle': 
            lanes_played[p] = mid_played

        if p == 'Jungle': 
            lanes_played[p] = Jungle_played

        if p == 'Support': 
            lanes_played[p] = Support_played

        if p == 'ADC': 
            lanes_played[p] = ADC_played
    
    return(lanes_played)
        
total = lane_input()
print(total)

Output

insert the number of times top played 1
insert the number of times Middle played 2
insert the number of times Jungle played 3
insert the number of times Support played 4
insert the number of times ADC played 5
{'Top': 1, 'Middle': 2, 'Jungle': 3, 'Support': 4, 'ADC': 5}

And there is no need to iterate through a dictionary, so you could refactor your code to this (with the same output)

def lane_input():
    lanes_played = {"Top":0, "Middle":0, "Jungle":0, "Support":0, "ADC":0}

    top_played = int(input("insert the number of times top played"))
    lanes_played['Top'] = top_played
    
    mid_played = int(input("insert the number of times Middle played"))
    lanes_played['Middle'] = mid_played
    
    Jungle_played = int(input("insert the number of times Jungle played"))
    lanes_played['Jungle'] = Jungle_played
    
    Support_played = int(input("insert the number of times Support played"))
    lanes_played['Support'] = Support_played
    
    ADC_played = int(input("insert the number of times ADC played"))
    lanes_played['ADC'] = ADC_played

    return(lanes_played)
        
total = lane_input()
print(total)

Your question has been answered. This answer just gives a better alternative for your code.

def lane_input():

        top_played      =   int(input("insert the number of times top played"))
        mid_played      =   int(input("insert the number of times Middle played"))
        Jungle_played   =   int(input("insert the number of times Jungle played"))
        Support_played  =   int(input("insert the number of times Support played"))
        ADC_played      =   int(input("insert the number of times ADC played"))

        return {
            "Top"       :   top_played,
            "Middle"    :   mid_played, 
            "Jungle"    :   Jungle_played, 
            "Support"   :   Support_played, 
            "ADC"       :   ADC_played
        }

Related