Python 如何在某一点停止范围功能

Python 如何在某一点停止范围功能,python,python-3.x,list,dictionary,range,Python,Python 3.x,List,Dictionary,Range,我想给键(在本例中为整数,从1开始)的值指定输入的确切数量。例如,如果有2个用户,我希望输出为:{1:{info},2:{info} 我的问题是有没有办法让它像那样工作?我目前正在尝试使用range()函数(我给出了21作为停止参数,因为可以输入的最大用户信息量是21)。我试过很多东西,但都不管用。如果有人能帮我,我将不胜感激 emptyDict = [] scores = {} age = {} adLoop = True while adLoop: user_input_name

我想给键(在本例中为整数,从1开始)的值指定输入的确切数量。例如,如果有2个用户,我希望输出为:{1:{info},2:{info}

我的问题是有没有办法让它像那样工作?我目前正在尝试使用range()函数(我给出了21作为停止参数,因为可以输入的最大用户信息量是21)。我试过很多东西,但都不管用。如果有人能帮我,我将不胜感激

emptyDict = []
scores = {}
age = {}

adLoop = True
while adLoop:
    user_input_name = input("Your name: ")
    user_input_age = int(input("Your age: "))
    user_input_score = int(float(input("Your score: ")))
    result = {"name": user_input_name, "age": user_input_age, "score": user_input_score}
    emptyDict.append(result)
    user_input_continue = input("Would you like to enter your info?: Y/N: ").lower()
    if user_input_continue == "y":
        adLoop = True
    elif user_input_continue == "n":
        adLoop = False
        print(emptyDict)
        if len(emptyDict) == 20:
            adLoop = False
            print(emptyDict)


for i in emptyDict:
    test = range(1, 21)
    for b in test:
        scores[b] = i

print(scores)


我不知道你为什么不能把它保存在列表中并使用它


使用
enumerate()
获取列表中的项目及其编号(+
enumerate(…,start_值)

或更短

scores.update(enumerate(emptyDict, 1))
或者在开始时不创建空dict

score = dict(enumerate(emptyDict, 1))

你的最后一个循环是错误的。您应该使用range确定“emptyDict”的长度,然后在循环中为分数添加一个键、值对

for i in range(0,len(emptyDict)):                                                                                        
    scores[i] = emptyDict[i]
print(scores)

范围在最后一个值之前结束。把它改成22。啊,我知道,但这不是我的问题。我希望它停止,以防有2个用户。当我使用21时,输入等于从1到21的每个整数。为什么不使用
枚举
,我有点搞不清楚嵌套for循环在做什么。你只能从
emptyDict
中获取最后一个值,如果你可以将它保存在
emptyDict
中,并使用
emptyDict[0]
而不是
score[1]
来获取所需的值,我不知道你为什么需要这把钥匙。如果你真的需要它,那么
对于b,我在enumerate(emptyDict,1):scores[b]=I
。或者更短的
分数。更新(枚举(emptyDict,1))
有比
range(len())更好的方法。
for i in range(0,len(emptyDict)):                                                                                        
    scores[i] = emptyDict[i]
print(scores)