mercredi 27 mai 2020

How to make an if statement works only once

I've made a simple if statement in Python 3 that checks if there's a specific word inside a line and if so it changes the value of the phrase. In the example below I'm showing you the code for the comma. Problem is that I need this statement to apply only once per line and I'm getting mad while trying to implement it.

That's the code

temp1 = ""
for line in testo.splitlines():
    n = len(line)
    if len(line) > 0:
        line = line[0].upper()+line[1:]
    if n >= 30 and ', ' in line and line.find(', ') >= 20:
        line = re.sub(', ', '\n', line)

    temp1 += line + "\n"
testo = temp1

This is the phrase

Let me see your hard work, come on, work harder

At the moment the code split the phrase like this

Let me see your hard work
Come on
Work harder

What I wish to happen is to have the phrase splitted this way (the if statement applies only once)

Let me see your hard work
Come on, work harder

Thank you everybody!

Aucun commentaire:

Enregistrer un commentaire