0

How would I make only 20 values appear in each line

smaller = 2 higher = 1000 print("Prime numbers between", smaller, "and", higher, "are:") for num in range(smaller, higher + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

13th Nov 2020, 10:26 AM
MA3
MA3 - avatar
4 Answers
+ 5
MA3 , you can use a list as a temporary buffer, that will be filled with numbers until 20 numbers are stored in it. Then print the buffer and reset it: smaller = 2 higher = 1000 buf = [] print("Prime numbers between", smaller, "and", higher, "are:") for num in range(smaller, higher + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: if len(buf) < 20: buf.append(str(num).rjust(4,' ')) #buf.append(num) else: print(*buf) buf =[]
13th Nov 2020, 4:04 PM
Lothar
Lothar - avatar
+ 1
Creat a blank string and instead of print(num) add the mum to the string and split at the end or create a list and append the num. Divide and find remainder of length by 20 and use this in a loop to print 20 per line.
13th Nov 2020, 11:26 AM
Olivia
Olivia - avatar
0
What do you mean exactly? You want one string with 20 prime numbers?
13th Nov 2020, 11:12 AM
Olivia
Olivia - avatar
0
yes please
13th Nov 2020, 11:21 AM
MA3
MA3 - avatar