-3

I'm new to coding and it's a little frustrating at the moment because i have to do an assessment where it outputs "1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz" and no i cant use print.

Code:

import sys
num1=int(input('Enter your number range: '))
for x in range(1,num1+1):
   if x % 3 == 0:
      print("Fizz",end=" ")
   if x % 5 == 0:
      print('Buzz',end=" ")
   if x % 3 != 0 and x % 5 != 0:
      print(str(x),end=" ")

Result: "enter your number range?"(well say 20)

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz 

so as you can see its on the same line but i don't want spaces, and i want commas in between each number and letter accept where number 15 should be, i want that to one word "FizzBuzz", as seen on the example :

1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
Phayer
  • 45
  • 1
  • 1
  • 9
  • You cannot use print? You mean that You cannot just print this string or You cannot use it anywhere in Your program? – Dzarafata May 12 '15 at 04:28
  • OK, I think you're almost there. What does `end=" "` do in your `print` calls? What test can you do to see if you should be printing "FizzBuzz" as one word? – Marius May 12 '15 at 04:29
  • When i use end="", it pulls the line bellow it up so instead of displaying on different lines it brings it up to the same line – Phayer May 12 '15 at 04:35
  • Right, so instead of each string you print ending with a newline (`\n`), it ends with a space. Can you see how to use that to get commas instead of spaces between your values? – Marius May 12 '15 at 04:38
  • i found one way but it futs a comma between every word and number, which is party what i want but i need number 15 to be the full word "FizzBuzz", also i still have spaces in-between my words and numbers – Phayer May 12 '15 at 04:49

2 Answers2

3

Don't use print for formating, but simply join the different string elements. Enter this in the interpreter:

','.join([     'FizzBuzz' if not(i%15) 
          else 'Fizz'     if not(i%3)
          else 'Buzz'     if not(i%5)
          else str(i)
          for i in range(1,a+1)
         ])

or put the above line inside a print statement

FinalState
  • 91
  • 1
  • 4
2

This is basically the same question I have answered here. If you look at the documentation for print, it says you print a variable number of objects (*objects in the docs). print takes a sep keyword, which is the character(s) you want to separate your printed objects with.

If you modify you code slightly to collect the items-to-be-printed in a list first and then print all at once:

num1=int(input('Enter your number range: '))
items = list()
for x in range(1,num1+1):
    if x % 3 == 0:
        items.append("Fizz")
    if x % 5 == 0:
        items.append('Buzz')
    if x % 3 != 0 and x % 5 != 0:
        items.append(str(x))

print(*items, sep=',')

When you run it:

Enter your number range: 10
1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz

For the *objects-syntax you can take a look at this SO-question.


To account for numbers evenly divisible with both 3 and 5:

num1=int(input('Enter your number range: '))
items = list()
for x in range(1,num1+1):
    if x % 5 == 0 and x % 3 == 0:
        items.append('FizzBuzz')
    elif x % 3 == 0:
        items.append("Fizz")
    elif x % 5 == 0:
        items.append('Buzz')
    else:
        items.append(str(x))

print(*items, sep=',')
Community
  • 1
  • 1
RickardSjogren
  • 4,070
  • 3
  • 17
  • 26
  • thank you, its very close now i just need to figure out how to get that pesky number 15 to be one word not two, because multiple of 3 are "Fizz" . With multiples of 5 are "Buzz". If the number is a multiple of both 3 and 5 then its "FizzBuzz". – Phayer May 12 '15 at 05:52
  • Thanks everyone for your contribution, now it;s working %100. can i just ask what (in line 3) "(1,num1+1)" the first 1 does to the code? – Phayer May 13 '15 at 00:10