2

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

13
  • How many times do you want to take input? Commented Jun 30, 2020 at 9:40
  • Could you maybe add pseudo code for your expected output? Commented Jun 30, 2020 at 9:41
  • no i want i to start again and again and not look like b is bigger than a b is bigger than a b is bigger than a Commented Jun 30, 2020 at 9:42
  • 3
    It is not clear what are you trying to say. What do you want as output? Commented Jun 30, 2020 at 9:42
  • 1
    @snoopstick Your code is correct then because it is doing the same thing that you want and if you want to clear the screen you can use import os and os.system('cls') Commented Jun 30, 2020 at 9:52

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

No need for continue. That happens automatically at the end of the loops body. And if isn't a function, so if(i==1): should be written as if i == 1:.
Oh yess, I guess I'm still stuck with C syntax
1

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')

1 Comment

Something you might want to address: The message is wrong if you enter the same number twice.

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.