0

I don't want the empty line to be printed in the output

I have tried end="" but since print is in loop, it is not working

def main():
    a=int(input())
    b=int(input())
    for i in range(a,b+1):
        prime=0
        for x in range(2,i):
            if(i%x==0):
                prime=prime+1
            else:
                prime=prime+0
        if(prime==0):
            print(i, end="") 
main()

Actual result:

907
911
919
929
937
941
947
953
967
971
977
983
991
997

Expected:

907
911
919
929
937
941
947
953
967
971
977
983
991
997
hem
  • 1,012
  • 6
  • 11
Charu
  • 40
  • 4
  • It is hard to distinguish your expected result from your actual result. Also, your code prints no newlines at all, so neither your expected nor your actual output match the code. – John Coleman Jul 03 '19 at 16:19
  • With that code, all the numbers should be written on one line without any spaces. Are you adding newlines somehow? If the numbers are all on their own line then naturally the last one will end with one as well – Kelo Jul 03 '19 at 16:24

3 Answers3

0

You might want to look at this: how can I remove a trailing newline. It states to use .rstrip() but you should read the docs to make sure.

Community
  • 1
  • 1
hello_there
  • 209
  • 1
  • 5
  • 10
0

One possibility is to assemble the results into a list and print that list (joined by '\n') at the end:

def main():
    a=int(input())
    b=int(input())
    results = []
    for i in range(a,b+1):
        prime=0
        for x in range(2,i):
            if(i%x==0):
                prime=prime+1
            else:
                prime=prime+0
        if(prime==0):
            results.append(i)
    print('\n'.join(str(i) for i in results))
main()  

I am not completely sure what you are trying to do, you could also experiment with replacing the print statement in the above code by

print('\n'.join(str(i) for i in results), end = '')  
John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • print('\n'.join(str(i) for i in results), end = '') Thanks, This statement worked out. Can u explain it a bit? How it didnt printed another line at the last – Charu Jul 03 '19 at 17:08
0

You can dynamically print things out like this

for i in range(10):
    print(i, sep=' ', end='', flush=True)

This will output:

123456789

see Print in one line dynamically for more algorithms that might fit what you need!

Engineero
  • 12,340
  • 5
  • 53
  • 75
raceee
  • 477
  • 5
  • 14