循环遍历具有'for'循环的字典会过早中断


import itertools
print "Hello and welcome to the questionnaire software"
def list_to_dict(l):
    d = dict(itertools.izip_longest(*[iter(l)] * 2, fillvalue=""))
    return d
activities = ["Go Shopping","Sleep","Read a Book"]
def choosing():
    print "This questionnaire is intended to help two people choose what to do without offending one another"
    print "Here is a list of suggested activities: " 
    print ", ".join(activities)
    add_more = str(raw_input("Would you like to add more? Y/N"))
    if add_more in ["Yes","yes","Y","y","YES"]:
        while 1:
            addition = str(raw_input("Please type your addition here, type STOP to finish: "))
            if addition.lower() == "stop":
                print "Thank you for your suggestions (If any)"
                break
            else:
                activities.append(addition)
        print "Here are your new activities: "
        print ", ".join(activities)
    elif add_more in ["NO","no","No","n","N"]:
        print "okay he we go....."
    else:
        print "sorry, that was not a valid answer, please just type "Y" for yes or "N" for no"
    dict_activities= list_to_dict(activities)
    for i in dict_activities:
        dict_activities[i] = int(raw_input("Person 1 please give a score between 1 and 10 how much would you like to: " + i )) + int(raw_input("Person 2 please give a score between a score of 1 and 10 how much would you like to: " + i))
    for i in dict_activities:
        if dict_activities[i]>=12:
            print i 

choosing()

当我在cmd中运行代码时,我可以达到

for i in dict_activities:
            dict_activities[i] = int(raw_input("Person 1 please give a score between 1 and 10 how much would you like to: " + i )) + int(raw_input("Person 2 please give a score between a score of 1 and 10 how much would you like to: " + i))

它会提示输入键,但在到达最后之前突然停止2个键,知道的原因吗

控制台打印输出的示例:

Hello and welcome to the questionnaire software
This questionnaire is intended to help two people choose what to do without offe
nding one another
Here is a list of suggested activities:
Go Shopping, Sleep, Read a Book
Would you like to add more? Y/Ny
Please type your addition here, type STOP to finish: Option A
Please type your addition here, type STOP to finish: Option B
Please type your addition here, type STOP to finish: Option C
Please type your addition here, type STOP to finish: stop
Thank you for your suggestions (If any)
Here are your new activities:
Go Shopping, Sleep, Read a Book, Option A, Option B, Option C
{'Go Shopping': 'Sleep', 'Read a Book': 'Option A', 'Option B': 'Option C'}
between a score of 1 and 10 how much would you like to: Go Shopping? 6
between a score of 1 and 10 how much would you like to: Go Shopping? 7
between a score of 1 and 10 how much would you like to: Read a Book? 4
between a score of 1 and 10 how much would you like to: Read a Book? 3
between a score of 1 and 10 how much would you like to: Option B? 9
between a score of 1 and 10 how much would you like to: Option B? 9
Go Shopping
Option B

这些是我必须添加的更多细节才能发布这篇文章,我对所有这些感到抱歉,如果这篇文章太详细,我深表歉意。我真的是!

当您在迭代dict(或列表等(时修改时,会混淆解释器。您应该只对副本进行迭代。

在这种情况下,由于您已经有了一个密钥列表,您不需要修改它,所以我只需要放弃list_to_dict(),并更改这两行:

dict_activities= list_to_dict(activities)
for i in dict_activities:

至:

dict_activities= {}
for i in activities:

(未测试(。(在这种情况下也不需要import itertools。(

最新更新