Home > Back-end >  Printing a range on the same line, and ending with a newline (Python)
Printing a range on the same line, and ending with a newline (Python)

Time:11-12

I am working on a homework assignment, and I managed to get most of the code (at least I think so). But when I go to submit the assignment, my textbook wants my output to end in a newline.
Assignment: Write a program whose input is two integers. Output the first integer and subsequent increments of 5 as long as the value is less than or equal to the second integer.

Ex: If the input is:

-15 10 the output is:

-15 -10 -5 0 5 10 Ex: If the second integer is less than the first as in:

20 5 the output is:

Second integer can't be less than the first.

My code:

x = int(input())
y = int(input())

if y >= x:
    for i in range(x, y 1, 5,):
        print(i, end=' ')
else:
    print('Second integer can\'t be less than the first.')

This is where my problem lies.

I'm still new to python, and coding in general. So any help/explanations would be greatly appreciated!

Edit: This is the code that worked. Thanks guys!

x = int(input())
y = int(input())

if y >= x:
    for i in range(x, y 1, 5,):
        print(i, end=' ')
    print()
else:
    print('Second integer can\'t be less than the first.')

CodePudding user response:

Add a print('\n') after your loop

if y >= x:
    for i in range(x, y 1, 5,):
        print(i, end=' ')
    print('\n')

CodePudding user response:

Add print() after the loop:

x = int(input())
y = int(input())

if y >= x:
    for i in range(x, y 1, 5,):
        print(i, end=' ')
    print()
else:
    print('Second integer can\'t be less than the first.')
  • Related