Reputation: 603
I want to print inside a while loop only once. As shown below:
# Starting a while-loop
while True:
# Asking for input(a and b)
a = int(input('enter a number'))
b = int(input('enter another number'))
# If a is bigger than print('bigger than b')
if a>b:
print('a is bigger then b')
# Otherwise...
else:
print('b is bigger than a')
# Start all over again from a = int(input('enter a number'))
Now I don't want to entirely stop the loop using break
or condition = True
. I just want the loop to start all over again.
If you want any clarifications, feel free to ask
Upvotes: 2
Views: 4711
Reputation: 711
Use a counter that will input values continuously and print only once.
i=1
while True:
# Asking for input(a and b)
a = int(input('enter a number'))
b = int(input('enter another number'))
if i==1:
i=i+1
if a>b:
print('a is bigger then b')
else:
print('b is bigger than a')
else:
continue
Upvotes: 1
Reputation: 2518
To execute some block of code only once within an endless while
loop, you need to use a flag like in the example below:
printed_flag = False
while True:
a = int(input('enter a number'))
b = int(input('enter another number'))
if printed_flag:
if a>b:
print('a is bigger then b')
else:
print('b is bigger than a')
printed_flag = False
Upvotes: 3
Reputation: 13
You can add in a counter to repeat the loop until the counter stops. What was done below is to set the counter to infinity.
i = 0
while i <= float("inf"):
i += 1
a = int(input('enter a number'))
b = int(input('enter another number'))
if a>b:
print('a is bigger then b')
else:
print('b is bigger than a')
Upvotes: 1