I was trying to write a simple Python 3 program and cant find answers.
fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]
for i in fruits:
if fruits[i] in favoritefruits:
print("I'm gonna buy some " fruits[i] " because they are one of my favorite fruits.")
else:
print("I'm not going to buy " fruits[i] ", I don't like them.")
CodePudding user response:
As @wkl has stated, for i in fruits
will iterate through all fruit names, not the index. Replacing fruits[i]
with i
(though a more descriptive name is better) will fix the issue:
fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]
for fruit in fruits:
if fruit in favoritefruits:
print("I'm gonna buy some " fruit " because they are one of my favorite fruits.")
else:
print("I'm not going to buy " fruit ", I don't like them.")
CodePudding user response:
You Have Two Lists
fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
Also
favoritefruits = [fruits[0], fruits[2], fruits[3], fruits[7], fruits[8], fruits[13]]
To iterate you do not need to use i
. Just use
for fruit in fruits:
if fruit in favoritefruits:
print("I'm gonna buy some " fruit " because they are one of my favorite fruits.")
else:
print("I'm not going to buy " fruit ", I don't like them.")
Output:
I'm gonna buy some strawberries because they are one of my favorite fruits.
I'm not going to buy apples, I don't like them.
I'm gonna buy some bananas because they are one of my favorite fruits.
I'm gonna buy some pomegranates because they are one of my favorite fruits.
I'm not going to buy blueberries, I don't like them.
I'm not going to buy dragon fruits, I don't like them.
I'm not going to buy papayas, I don't like them.
I'm gonna buy some pears because they are one of my favorite fruits.
I'm gonna buy some oranges because they are one of my favorite fruits.
I'm not going to buy mango, I don't like them.
I'm not going to buy tomatoes, I don't like them.
I'm not going to buy peaches, I don't like them.
I'm not going to buy melons, I don't like them.
I'm gonna buy some watermelons because they are one of my favorite fruits.
CodePudding user response:
for i in fruits
will return the fruits themselves and not the index and so when you do fruits[i]
python expects an int but you gave it a string. You can do for i in range(len(fruits))
so that fruits[i]
returns the fruit but in your code you only use the index to get the fruit so a much cleaner solution would be to rewrite for i in fruits
into for fruit in fruits
and simply use fruit
inside the loop.
CodePudding user response:
I think this approach might be useful:
First, if you are getting a fruit, and the list changes then favorite_fruits
now has an offset of at least 1. To solve this problem, you can do:
def get_favourites(favourites):
""" Get favourite fruits from a list """
new_favourite_fruits = []
for fruit in favourites:
try:
new_favourite_fruits.append(fruits.index(fruit))
except IndexError:
print("Couldn\'t find an {fruit} in the list of fruits :( ")
return new_favourite_fruits
So if you were to apply this to your original code:
fruits = ["strawberries", "apples", "bananas", "pomegranates", "blueberries", "dragon fruits", "papayas", "pears", "oranges", "mango", "tomatoes", "peaches", "melons", "watermelons"]
favourite_fruits = get_favourites(['apples','mango','melons'])
for fruit in new_favourite_fruits(['apples','oranges','peaches']):
print(f"I\'m going out to buy a {fruit}")
Also, depending on how much you work with lists you might find this useful:
for item, fruit in enumerate(fruits):
print(f"I am going to buy a {fruit} at {item}")
The advantage of enumerate
is it allows you to get both the index, and the fruit at the same time.