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)
4 Respostas
+ 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 =[]
+ 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.
0
What do you mean exactly?
You want one string with 20 prime numbers?
0
yes please