[Python] empty string returns True upon checking if its contained in a non empty string -...

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    This code is meant to count all the words that have the expression "di" but with the text "imagina." final answer is 1.

    texto = 'imagina.'
    cl = 0
    flag_di = False
    answer = 0
    previous = ''

    for car in texto:
    if car != ' ' and car != '.':
    cl += 1


    if car in 'iI' and previous in 'dD':
    flag_di = True

    previous = car

    else:
    if car == ' ' or car == '.':
    if flag_di:
    answer += 1

    cl = 0
    flag_di = False
    previous = ''

    print(answer)

    Continue reading...

Compartilhe esta Página