通过字典迭代' for'循环中断过早

时间:2014-04-23 00:36:32

标签: python python-2.7 for-loop dictionary

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

这些是我必须添加的更多细节,允许发布此内容,我很抱歉这一切,如果这篇文章太详细,我道歉。我真的是!

1 个答案:

答案 0 :(得分:2)

当你在迭代它时修改 dict(或列表等) 时,你会混淆解释器。你应该只迭代副本。

在这种情况下,既然你已经有了一个你不会修改的密钥列表,我就会抛弃list_to_dict(),并改变这两行:

dict_activities= list_to_dict(activities)
for i in dict_activities:

为:

dict_activities= {}
for i in activities:

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