Home > Blockchain >  How do I loop through this dictionary correctly in python?
How do I loop through this dictionary correctly in python?

Time:12-03

favorite_foods = {'bill': 'cake', 'alex': 'patacones'}

for name in  favorite_foods:
    print(f"I dont agree with your favorite food {name.title()}.")
    
    for food  in (favorite_foods.values()):
        print(f"{food.title()} is delicious, but not that good!")
    if food in (favorite_foods.values() endswith(s)
        print(f"{food.title()} are delicous, but not that good!")

How do I loop through this dictionary correctly?

I want it to say

I dont agree with your favorite food Bill. Cake is delicious but not that good!

I dont agree with your favorite food Alex. Patacones are delicious, but not that good!

I appreciate all the help. Thank you.

The code cycles through all of the values instead of stopping after one. I googled the endswith function to see if I could get the code to print something different if the value ended in 's' but it didnt work. Before I added that line it printed the following.

I dont agree with your favorite food Bill. Cake is delicious, but not that good! Cake are delicous, but not that good! Patacones is delicious, but not that good! Patacones are delicous, but not that good! I dont agree with your favorite food Alex. Cake is delicious, but not that good! Cake are delicous, but not that good! Patacones is delicious, but not that good! Patacones are delicous, but not that good!

I wanted to find a way to trigger "are" when the value was plural and "is" if the value was singular.

CodePudding user response:

you can use 2 things to make this correct and simpler.

Use for key, val in <dict_name>.items() to loop through key, value pairs at the same time.

Use ternary operator in cases when you need to choose a value based on a boolean. So for example print(("yum!" if tasteGood else "gross!")) will print "yum!" when tasteGood is True, but print "gross!" when tasteGood is false.

So to put everything together

favFoods = {
'bill':'cake','alex':'patacones'
}

for name, food in favFoods.items():
   print(f"I dont agree with your favorite food {name.title()}.")
   print(f"{food.title()} {('are' if food.endswith('s') else 'is')} delicious, but not that good!")

CodePudding user response:

You had a ton of syntax errors, and also your if statement was trying to be a for loop. This works:

favorite_foods = {'bill': 'cake', 'alex': 'patacones'}

for name in  favorite_foods:
    print(f"I dont agree with your favorite food {name.title()}.")
    
    for food  in (favorite_foods.values()):
        if food.endswith('s'):
            print(f"{food.title()} are delicous, but not that good!")
        else:
            print(f"{food.title()} is delicious, but not that good!")

CodePudding user response:

use .items() to loop through dict

favorite_foods = {'bill': 'cake', 'alex': 'patacones'}

output = []
for k,v in favorite_foods.items():
    output.append(f"I dont agree with your favorite food {k.title()}.")
    if v[-1] == 's':
        output.append(f"{v.title()} are delicious, but not that good!")
    else:
        output.append(f"{v.title()} is delicious, but not that good!")

print(" ".join(output))

output:

I dont agree with your favorite food bill. cake is delicious, but not that good! I dont agree with your favorite food alex. patacones are delicious, but not that good! 
  • Related