1
a = input().split()

for i in a:
    c = int(i)
    if c > 0:        
        b = c - 4
        d = b % 10
        if d != 0:
            print(i, "",  end='')
    else:
        e = c + 4
        f = e % 10
        if f != 0:
            print(i, "", end='')

This the whole code

The expected final output should be integers with space in between but no space at the end

By adding "" I got spaces in between the elements.

So, how do I delete the space after the final, last element.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Cameron
  • 11
  • 1
  • Does this answer your question? [Print a list of space-separated elements in Python 3](https://stackoverflow.com/questions/22556449/print-a-list-of-space-separated-elements-in-python-3) – Tomerikoo Feb 08 '21 at 13:59
  • FWIW, `e` and `f` are redundant for `b` and `d`. You could simplify by using a conditional expression like this: `b = c-4 if c>0 else c+4` – wjandrea Feb 08 '21 at 16:04

3 Answers3

1

I would suggest you store all you values to print in a list (so instead of print you append them to a list) and the at the end

print(*list)

Which will print each of the elements with a space in between.

Marsolgen
  • 199
  • 1
  • 8
1

You can append your results to a list and then print the list.

results = []
for i in a:
    c = int(i) 
    if c > 0:           
        b = c - 4    
        d = b % 10     
        if d != 0:        
            results.append(i)
    else:   
        e = c + 4
        f = e % 10
        if f != 0:
            results.append(i)

print(*results)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
KJDII
  • 851
  • 4
  • 11
0

Or, if you do not want to create a list, you can count the elements, and use an if else statement to determine if it's the last element or not.

a = input().split()

for counter, i in enumerate(a):
    c = int(i)
    if c > 0:        
        b = c - 4
        d = b % 10
        if d != 0:
            if counter < len(a) - 1:
                print(i, "",  end='')
            else:
                print(i, end='')
    else:
        e = c + 4
        f = e % 10
        if f != 0:
            if counter < len(a) - 1:
                print(i, "", end='')
            else:
                print(i, end='')
TheEagle
  • 5,808
  • 3
  • 11
  • 39