Harvs
Harvs

Reputation: 533

Iterating through a dictionary with a 'for' loop breaks prematurely

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()

when i run the code in cmd, i get as far as

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))

it will give the input prompts for the keys, but then suddenly stop 2 keys before getting to the end, any idea why

an example of the console print out:

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

these are more details i have to add to be allowed to post this, im sorry for all this and i apologise if this post was too detailed. i really am!

Upvotes: 0

Views: 74

Answers (1)

William McBrine
William McBrine

Reputation: 2256

When you modify the dict (or list, or etc.) while iterating over it, you confuse the interpreter. You should only iterate over a copy.

In this case, since you already have a list of the keys, which you don't modify, I'd just ditch list_to_dict(), and change these two lines:

dict_activities= list_to_dict(activities)
for i in dict_activities:

to:

dict_activities= {}
for i in activities:

(not tested). (You don't need import itertools in that case, either.)

Upvotes: 2

Related Questions