循环嵌套问题,项目未正确引用
Loop Nesting Issue, Items not Properly Referenced
我编写了一个程序,它采用三个样本 objects 的元组(不确定它们是否真的 objects,但我在这里将它们标记为这样),并进行比较元组或其中的项目到“收藏夹”元组。然而,当我 运行 这个时:
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()
但是,当我这样做时,它总是打印预期的消息两次,一次用于第一项,一次用于第二项。
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
如果我continue
每次到达第二个项目时,它就将其从评分系统中取出,只检查第一个项目,反之亦然。有没有一种方法可以检查满足 if item in favorites
条件的两个项目,只打印一行?
您可以重构您的代码以使用单个循环和 zip
+map
+sum
来计算匹配数。
请注意,在您的代码中,您还尝试将食物类型(例如三明治)与口味(例如酸味)相匹配,反之,这可能是不可取的。此代码不执行此操作。
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()
输出:
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!
使用的输入:
favorites = ('sweet', 'sandwiches')
foods = (('salty', 'pizza'),
('sweet', 'sandwiches'),
('sour', 'sandwiches'))
我编写了一个程序,它采用三个样本 objects 的元组(不确定它们是否真的 objects,但我在这里将它们标记为这样),并进行比较元组或其中的项目到“收藏夹”元组。然而,当我 运行 这个时:
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()
但是,当我这样做时,它总是打印预期的消息两次,一次用于第一项,一次用于第二项。
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
如果我continue
每次到达第二个项目时,它就将其从评分系统中取出,只检查第一个项目,反之亦然。有没有一种方法可以检查满足 if item in favorites
条件的两个项目,只打印一行?
您可以重构您的代码以使用单个循环和 zip
+map
+sum
来计算匹配数。
请注意,在您的代码中,您还尝试将食物类型(例如三明治)与口味(例如酸味)相匹配,反之,这可能是不可取的。此代码不执行此操作。
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()
输出:
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!
使用的输入:
favorites = ('sweet', 'sandwiches')
foods = (('salty', 'pizza'),
('sweet', 'sandwiches'),
('sour', 'sandwiches'))