【问题标题】:Iterating through a dictionary with a 'for' loop breaks prematurely使用“for”循环遍历字典过早中断
【发布时间】:2014-06-07 14:32:40
【问题描述】:
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

这些是我必须添加的更多细节才能发布此内容,对此我深表歉意,如果此帖子过于详细,我深表歉意。我真的!

【问题讨论】:

    标签: python python-2.7 for-loop dictionary


    【解决方案1】:

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

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

    dict_activities= list_to_dict(activities)
    for i in dict_activities:
    

    到:

    dict_activities= {}
    for i in activities:
    

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

    【讨论】:

      猜你喜欢
      • 2013-02-20
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多