Newbie
Newbie

Reputation: 21

How to run a action 1 time ONLY in while loop

I want to run 1 time ONLY in while loop.

while True:
    print("Hello World")#this word always print
    print("HI")#this word print 1 time only

Upvotes: 1

Views: 1797

Answers (2)

crimson589
crimson589

Reputation: 1306

Just add a validation logic to it

x = False
while True:
    print("Hello World")
    if x == False:
        print("HI")
        x = True

Upvotes: 2

mcdev
mcdev

Reputation: 111

I have no idea what your use case is... but just add a "break" to end the loop.

while True:
    print("Hello World")
    print("This words should show 1 time")
    break

Edit based on your comment...

ran = False
while True:
    print("Hello World")
    If not ran
        print("This words should show 1 time")
        ran = True

Upvotes: 3

Related Questions