How do you know if the phrase is on the list?

Asked

Viewed 48 times

0

I want the message "Uhul! Your secret friend will love", but the variable present always gives as answer "Try Again", example: in the variable 'personal gifts' I type 'pedro camisa keyboard chair', in the variable present when I typed 'pedro camisa' was to say that I hit, but it says "Try Again", how to fix this error in my code.

Code:

participantes = int(input('Número de participantes: '))
lista_pessoas = []
for p in range(participantes):
    pessoa_presentes = input('Digite seu nome e os 3 presentes desejados: ')
    lista_pessoas.append(pessoa_presentes)
print(lista_pessoas)
presente = ''
while presente != 'FIM':
    presente = input('Qual presente você deseja dar: ')
    if presente != 'FIM':
        if presente in lista_pessoas:
            print('Uhul! Seu amigo secreto vai adorar')
        else:
            print('Tente Novamente!')

1 answer

0

Good night Pedro.

What you’re doing won’t work because you’re not checking the variable presente with another string but with the generated people list lista_pessoas for this you need to iterate each record in this list. Another obstacle is that the operator in it will only work if the words are in the same sequence if your string search was "keyboard peter" would not work, so I suggest you use the split method as follows.

while presente != 'FIM':
    presente = input('Qual presente você deseja dar: ').split(' ')
    if presente != 'FIM':
        for i in lista_pessoas:
            pessoa_presentes = i.split(' ')
            if pessoa_presentes[0] == presente[0] and presente[1] in  pessoa_presentes[1:]:
                print('Uhul! Seu amigo secreto vai adorar')
        else:
            print('Tente Novamente!')

So if the inputs have a default you will use the first record to compare the names and the subsequent ones to would be to check if the present was suggested

  • In this code the exit, when we hit, is coming out again and again and the loop never ends.

  • Exactly, you can put presente = 'FIM' or simply use the command break after the print

Browser other questions tagged

You are not signed in. Login or sign up in order to post.