-2

example:

i = 'string'
for x in i:
    print('hi this should print multiple times')
    #somthing that detects when for loop is finished
#or something that detects that here

its quite simple, all examples i found either didn't work/were really complex, and i'm just looking for something simple bc im sure theres a good way to do this and i just didn't find it.

links ive found but have not realy worked for me:

this page, this page

1 Answers1

1

What are you trying to achieve? If you just want to run some code after the for loop is done, you can just put that piece of code after the for loop.

If you would like to check whether the loop completed "normally" (without encountering a break statement), you can use the for/else statement:

    for x in range(2, n):
        if n % x == 0:
            print( n, 'equals', x, '*', n/x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')

Source: https://book.pythontips.com/en/latest/for_-_else.html

adamgy
  • 4,543
  • 3
  • 16
  • 31