dataset = ['Apple', 'Banana', 'Mango', 'Grape', 'Pineapple',
'Lettuce', 'Cabbage', 'Spinach', 'Carrot', 'Cauliflower']
ran = random.choice(dataset)
fruit = ['Apple', 'Banana', 'Mango', 'Grape', 'Pineapple']
vegetable = ['Lettuce', 'Cabbage', 'Spinach', 'Carrot', 'Cauliflower']
if ran == vegetable:
print('HINT: It is a vegetable!')
else:
print('HINT: It is a fruit!')
First I categorized items in a list but when I run my code it would only say the else statement.
CodePudding user response:
You have to check if it is in the list, an example:
import random
dataset = ['Apple', 'Banana', 'Mango', 'Grape', 'Pineapple',
'Lettuce', 'Cabbage', 'Spinach', 'Carrot', 'Cauliflower']
ran = random.choice(dataset)
fruit = ['Apple', 'Banana', 'Mango', 'Grape', 'Pineapple']
vegetable = ['Lettuce', 'Cabbage', 'Spinach', 'Carrot', 'Cauliflower']
print(ran) // To verify the item
if ran in fruit:
print("Fruit")
elif ran in vegetable:
print("Vegetable")
else:
print("Unknown")
CodePudding user response:
I think its because your asking if the random element, is equal to the entire "Vegetable" array, and in this case it is not, so it will return false.
Do a for loop where you loop over each element of the vegetable array (Or use an inbuilt function of a library to check if the ran elemet is contained in the vegetable array), and check the element againsed your ran variable, then if it is equal, you can print('Hint: it is a vegetable') and break out of the loop, if its not, your else statement will trigger and run: print('HINT: It is a fruit!') :)
The only case it should return "Vegetables, (in the provided information) is if ran = ['Apple', 'Banana', 'Mango', 'Grape', 'Pineapple', 'Lettuce', 'Cabbage', 'Spinach', 'Carrot', 'Cauliflower'] (The vegetable array/dataset)