0

i'm self learning python through out online courses but i ran into a problem .

for loop

how do i tell python to do something after the for loops is finished ?

for example :

def mixer(g):
print(g)

def printer(h):
    print("end")

X = input ("words :  ") 
var2 = ""
var1 = X.split()


for y in var1:
    if len(y) > 5:
        y.upper()
        var2 += y.upper() + " "
        mixer(var2)
    elif len(y)  <= 5:
        y.lower()
        var2 += y.lower() + " "
        mixer(var2)

that's what i'm trying to do after for loop is done , i want to call a function after the loop , how can u do this ? ## >>> this method didn't work

    elif y == False:
        print("false")
        printer(var2)
3
  • Just put the code after the for-loop? Commented May 30, 2018 at 23:40
  • 3
    make sure you un-indent the block of code you want executed after the for-loop. Python uses indentation to figure out what blocks of code belong to a loop or not. Commented May 30, 2018 at 23:42
  • FYI, when the condition in elif is the exact opposite of the if condition, you should use else:. Commented May 31, 2018 at 0:43

1 Answer 1

1

Just put it outside the body of the for loop:

for x in y:
    ... indented block does stuff with x on each loop ...
... dedented code (outside loop body) run once after loop is finished ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.