Write a program that displays, 10 numbers per line, all the numbers from 100 to 200 that are divisible by 5 or 6, but not both. The numbers are separated by exactly one space
My Code:
count = 0
for i in range (100, 201):
if (i %6==0 and i %5!=0) or (i%5==0 and i%6!=0):
print(str(i), "")
count = count + 1
if count == 10:
string = str(i) + str("")
print(string)
count = 0
Any way to put the numbers 10 per line?

Respuesta :

Answer & Explanation:

To print 10 on a line and each number separated by space; you make the following modifications.

print(str(i)+" ",end=' ')

str(i)-> represents the actual number to be printed

" "-> puts a space after the number is printed

end = ' ' -> allows printing to continue on the same line

if(count == 10):

     print(' ')

The above line checks if count variable is 10;

If yes, a blank is printed which allows printing to start on a new line;

The modified code is as follows (Also, see attachment for proper indentation)

count = 0

for i in range(100,201):

     if(i%6 == 0 and i%5!=0) or (i%5 ==0 and i%6!=0):

           print(str(i)+" ",end=' ')

           count = count + 1

           if(count == 10):

                 print(' ')

                 count = 0

Ver imagen MrRoyal