Python 函数 - 循环字典并更新值

标签 python python-3.x function loops dictionary

我遇到一个问题,我试图循环遍历字典以根据用户的特定输入更新所有值。

我在下面发布了我的代码,基本上,用户在不同的 channel 播放的输入框中输入一个数字,并且该函数应该更新字典以包含用户输入的值。

循环似乎并没有遍历我字典中的每个键,只返回第一个值,然后似乎跳过其余的值。

我只学习了 3 周的 Python 教程,但我不太确定我在这里做错了什么。

请参阅下面的代码:

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)

代码输出如下:

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}

最佳答案

这是因为代码中的缩进错误。 return 关键字在 for 循环的第一次迭代中被命中,这会使您的函数停止。

这是有效的,因为 return 发生在整个 for 循环之后:

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)

关于Python 函数 - 循环字典并更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67721733/

相关文章:

JavaScript - 为什么使用初始参数调用函数更快?

java - 在 Java 中将函数应用于 while 循环

python - pandas 在 python 中导入错误

Python - 用户输入变量名称作为参数传递给函数

python - 使用 cx_freeze 时遇到问题

python - 将数组的 Python 字典转换为数据框

python - 如何制作一个 Python 程序来演示 Zipf 定律?

javascript - Yii2按钮onclick匿名函数

javascript - 如何使 SJCL 和 Python hashlib 生成相同的 pdkdf2 输出

Python子进程模块将路径作为字符串发送