0

I just need to separate my out with a comma so it will print like this 1,2,fizz ect

for x in range (1, 21):
    if x%15==0:
        print("fizzbuzz",end=" ")
    elif x%5==0:
        print (("buzz"),end=" ") 
    elif x%3==0:
        print (("fizz"),end=" ")
    else:
        print (x,end=" ")

I can add a comma where " " is but my list will print with a comma at the end like 1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz,

I've gone over my notes and went on python tutorials but I am not sure how to get rid of the last comma or use a more effective method rather than just add that comma in instead of space.

I asked this before but I was confused by the wording so my question came out real confusing. I understand that this may be simple but this is my first time programming so I am a noob. My lecturer hasn't explained to me how I can do this. I could really use some help/ pointers. Thanks.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Jessica Smith
  • 45
  • 1
  • 2
  • 7

2 Answers2

5

Instead of printing them immediately, put everything is a list of strings. Then join the list with commas and print resulting string.

cababunga
  • 3,090
  • 15
  • 23
  • 1
    +1. Also, the functionality like in the question often becomes a function body. When writing functions, it is almost always better not to use `print()` inside. In theory, a function with `print` like calls is named as a *function with side effects*. It means it produces some output other than returning a value. In practice, functions with side effects are less universal. *Although practicality beats purity.* – pepr Aug 29 '13 at 12:23
4

This could be a good example for learning generators. A generator looks like a normal function that uses yield instead of return. The difference is that when generator function is used, it behaves as an iterable object that produces a sequence of values. Try the following:

#!python3

def gen():
    for x in range (1, 21):
        if x % 15 == 0:
            yield "fizzbuzz"
        elif x % 5 == 0:
            yield "buzz"
        elif x % 3 == 0:
            yield "fizz"
        else:
            yield str(x)


# Now the examples of using the generator.
for v in gen():
    print(v)

# Another example.
lst = list(gen())   # the list() iterates through the values and builds the list object
print(lst)

# And printing the join of the iterated elements.
print(','.join(gen()))  # the join iterates through the values and joins them by ','

# The above ','.join(gen()) produces a single string that is printed.
# The alternative approach is to use the fact the print function can accept more
# printed arguments, and it is possible to set a different separator than a space.
# The * in front of gen() means that the gen() will be evaluated as iterable.
# Simply said, print can see it as if all the values were explicitly writen as 
# the print arguments.
print(*gen(), sep=',')

See the doc for the print function arguments at http://docs.python.org/3/library/functions.html#print, and *expression call argument at http://docs.python.org/3/reference/expressions.html#calls.

Another advantage of the last print approach is that the arguments need not to be of the string type. The reason why the gen() definition explicitly used str(x) instead of plain x was because .join() requires that all joined values have to be of the string type. The print converts all the pased arguments to strings internally. If the gen() used plain yield x, and you insisted to use the join, the join could use a generator expression to convert the arguments to strings on the fly:

','.join(str(x) for x in gen())) 

It displays on my console:

c:\tmp\___python\JessicaSmith\so18500305>py a.py
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
['1', '2', 'fizz', '4', 'buzz', 'fizz', '7', '8', 'fizz', 'buzz', '11', 'fizz',
'13', '14', 'fizzbuzz', '16', '17', 'fizz', '19', 'buzz']
1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz
1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz
pepr
  • 20,112
  • 15
  • 76
  • 139
  • 2
    +1 for converting into a generator... but instead of using `','.join` make use of `print` and do `print(*gen(), sep=',')` instead – Jon Clements Aug 29 '13 at 13:55
  • 1
    It also means your generator needn't bother having to `yield str(x)` and just yield the number as it stands... – Jon Clements Aug 29 '13 at 14:27