compare from a list in python

2

I have a list which greetings

saludo = ['bien', 'genial']

What I am trying to do is that depending on the answer I get an argument, example:

if saludo[] == 'bien':
print('Me alegro')
else:
print('Que bueno que estes genial')

The problem arises that it does not give me any results. What am I doing wrong? I hope you help me, thank you!

    
asked by 19.01.2018 в 20:56
source

2 answers

4

Yes, from what I have understood in your comment, what you want is to search if the word is in the list, you can simply use in :

if 'bien' in saludo:
    print('Me alegro')
else:
    print('Que bueno que estes genial')
    
answered by 19.01.2018 / 21:26
source
0

If you want to compare an element of the array you can do it by indicating its index, remember that it starts with 0, for example, to compare the first element of the array:

saludo = ['bien', 'genial']

#primer elemento del array, indice 0.
if saludo[0] == 'bien': 
    print('Me alegro')
else:
    print('Que bueno que estes genial')

To make a comparison with all the elements of the array, obtain the elements of the array by means of a for and make the comparison, remember that the indentation is important:

saludo = ['bien', 'genial']
for i in range(len(saludo)):
    if saludo[i] == 'bien':
        print('Me alegro')
    else:
        print('Que bueno que estes genial')

you would have as output:

Me alegro
Que bueno que estes genial

here a online demo .

    
answered by 19.01.2018 в 22:43