Home > Software engineering >  Python- Printing sequence of integers separated by commas
Python- Printing sequence of integers separated by commas

Time:10-13

L=input().split(' ')
for i in L:
  c=float(i)
  print(int(c), sep=",")

A sequence of numbers separated by space is to be accepted from user, the numbers are converted to the greatest integer less than or equal to the number and then printed. I wanted to print the output on same line, but my code doesn't work

Thanks in advance....

CodePudding user response:

You are looping through the list, change to float and put it into variable c. That loop is essentially meaningless. After that loop c contains the last value in the iteration. Just print the result at once:

L=input().split(' ')
all_ints = map(int, L)
print(*all_ints, sep=',')

Or using str.join:

print(','.join(map(str, all_ints)))

Or using a loop:

for i in all_ints:
    print(i, end=',')

To get them all as floats you can use map as well:

all_floats = list(map(float, L))

CodePudding user response:

I think this should work for you:

L = input().split(' ')
for i in L:
  c = float(i)
  print(int(c), end=" ")
  • Related