Home > Back-end >  Loop Nesting Issue, Items not Properly Referenced
Loop Nesting Issue, Items not Properly Referenced

Time:01-02

I've written a program that takes the tuple of three sample objects (not sure if they're really objects, but I've labeled them as such here), and compares either the tuple or the items within it to the "favorites" tuple. When I run this, however:

import random

favorite_food = ['sushi', 'spaghetti', 'pizza', 'hamburgers', 'sandwiches']
favorite_taste = ['spicy', 'sweet', 'savory', 'sour', 'bitter', 'salty']
chosen_ff = random.choice(favorite_food)
chosen_ft = random.choice(favorite_taste)

test_food1 = ('salty', 'pizza')
test_food2 = ('sweet', 'sandwiches')
test_food3 = ('sour', 'sushi')
foods = (test_food1, test_food2, test_food3)
favorites = (chosen_ft, chosen_ff)

def foodresults():
    points = 0
    for food in foods:
        for item in food:
            print(food[0], food[1])
            if item in favorites:
                print("You got a match, nice job!  1 point")
                points = points   1
            elif food == favorites:
                print("Wow, it couldn't have enjoyed it more!  2 points")
                points = points   2
            else:
                print("It didn't like it very much...")

foodresults()

However, when I do so, it always prints the expected message twice, once for the first item and once for the second.

salty pizza
You got a match, nice job!  1 point
salty pizza
It didn't like it very much...
sweet sandwiches
It didn't like it very much...
sweet sandwiches
It didn't like it very much...
sour sushi
It didn't like it very much...
sour sushi
You got a match, nice job!  1 point

If I continue every time it reaches the second item, it takes it out of the scoring system and only checks the first item, and vice versa. Is there a way I can check for both items meeting the if item in favorites condition with it only printing one line?

CodePudding user response:

You could refactor your code to use a single loop and zip map sum to count the number of matches.

Note that in your code you also tried to match types of food (e.g. sandwich) with taste (e.g. sour) and conversely, which is probably not desirable. This code doesn't do this.

def foodresults():
    points = 0
    sentences = {0: "It didn't like it very much...",
                 1: "You got a match, nice job!  1 point",
                 2: "Wow, it couldn't have enjoyed it more!  2 points",
                }
    
    for food in foods:
        matches = sum(map(lambda x: x[0]==x[1], zip(favorites, food)))
        points  = matches
        print(*food)
        print(sentences[matches])

    print(f'You got a total of {points} points!')

foodresults()

output:

salty pizza
It didn't like it very much...
sweet sandwiches
Wow, it couldn't have enjoyed it more!  2 points
sour sandwiches
You got a match, nice job!  1 point
You got a total of 3 points!

used input:

favorites = ('sweet', 'sandwiches')
foods = (('salty', 'pizza'),
         ('sweet', 'sandwiches'),
         ('sour', 'sandwiches'))
  • Related