-1
start = int(input("Enter a number: "))

for number in range(1, start + 1):
    if (number % 2 == 0):
        print("{0}".format(number), end=",")

I have this code that prints out the right data but I don't want the input to be in the output and how to I remove the final comma. Plus how do I add the 0 at the beginning? How do I fix this?

Example if I input 10. The output needs to be 0,2,4,6,8

  • Can you clarify what the issue is? Please see [ask], [help/on-topic]. – AMC Sep 07 '20 at 23:42
  • Does this answer your question? [Python - printing out list separated with comma](https://stackoverflow.com/questions/32796452/python-printing-out-list-separated-with-comma) – Nick Sep 08 '20 at 07:25

6 Answers6

2

The following should be what you want I think.

start = int(input("Enter a number: "))
numbers = [number for number in range(start) if number%2==0]
print(*numbers, sep=", ")

Output

0, 2, 4, 6, 8  

A very similar question has been asked in so. You can find a detailed explanation here Python - printing out list separated with comma

Bhasfe
  • 21
  • 5
1

I would store the results in a list and then join them at the end:

start = int(input("Enter a number: "))
results = []

for number in range(0, start):
    if (number % 2 == 0):
        results.append(number)

print(",".join(map(str, results)))
flakes
  • 21,558
  • 8
  • 41
  • 88
  • I get this error when adding your changes Traceback (most recent call last): File "/Users/freed/Desktop/M3L4.py", line 15, in print(",".join(results)) TypeError: sequence item 0: expected str instance, int found –  Sep 07 '20 at 23:03
  • you can't join a string with a list like that. – de_classified Sep 07 '20 at 23:03
  • @FishingCode right you are. I updated the answer to transform the inputs to string. – flakes Sep 07 '20 at 23:05
  • the other simple approach @Freedom220, is to use a `while` loop right after the if statement to prevent including the `start` number i.e. 10 in your format. – de_classified Sep 07 '20 at 23:06
  • It works great, but how do I remove the final output and comma. –  Sep 07 '20 at 23:07
  • @Freedom220 Oh, you want to not add the plus one to the `range` method. That's why the last value is getting added. You can also start at zero. Will update – flakes Sep 07 '20 at 23:08
1

Starting from 0 and increasing to 2 may be useful.

start = int(input("Enter a number: "))
for number in range(0, start, 2):
    if (number >= start-2):
        print("{0}".format(number))
        break
    print("{0}".format(number), end=",")
Cycerman
  • 26
  • 4
1
>>> print(*range(0, start, 2), sep=',')
0,2,4,6,8
superb rain
  • 5,300
  • 2
  • 11
  • 25
0

You can try this..

start = int(input("Enter a number: "))

','.join([str(x) for x in range(start) if x % 2 ==0])

Result:

'0,2,4,6,8'
Prashy
  • 61
  • 6
0
start = int(input("Enter a number: "))
answer = ''
for number in range(1, start + 1):
    if (number % 2 == 0):
        answer += ("{0},".format(number))
if f"{start}," in answer:
    answer = answer.replace(f",{start},", '')
print(answer)