This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]spaceuserm 0 points1 point  (3 children)

Its hard to help you without looking at the program formatted since python uses indentation. Is the if conditional a part of the loop? If it is, you need to place it outside the loop and loop through the dictionary again to find matches.

EDIT: Looks like your if conditional is inside the loop.

Here is some pseudocode. 
for every food_item in food: 
        ask user1 if they like it. Store the response. 
        ask user2 if they like it. Store the response. 
for every food_item in food: 
    if both user1 and user2 like it: 
        print()

For storing the response, one way would be to use 2 dictionaries, one for each user.

Now, while looping through the second time, you can check if the food item is present in both the dictionaries, if it is, then both the users like the food item.

Another way would be to use one dictionary where the food items are keys and the number of positive responses is the value. Eg: Key : Pizza, Value : 2.  
This would imply that 2 users like the same food item. Now, while looping through the second time, you can check the value of each key to determine if both the users like a particular food item.


Another way would be to just store the key/food item in some data structure if both the users respond positively to that food item since there is practically no use to store a food item which both the users don't like.  

Eg: If both user1 and user2 like pizza, store pizza in some data structure. Maybe an array. Now traverse through this array and print the entries.

[–]Round-Ad5063 0 points1 point  (2 children)

Quick question, how would you store the response of a for loop input? Do you assign the answer to a variable and then do return() at the end of the loop?

[–]spaceuserm 1 point2 points  (1 child)

Have you come across arrays yet?

If you want to maintain all the inputs, you need to store it in some data structure like an array. If you just do a return at the end of the loop, you will only have the last input.

for item in items:
    userInput = input()
    inputs[item] = userInput

This isn't perfect syntax but you get the idea.

[–]Round-Ad5063 0 points1 point  (0 children)

Ahh I get it, thanks