-7

Write a simple function called fizzBuzz that takes in a positive integer, and returns ‘fizz’, ‘buzz’, ‘fizzbuzz’ or the argument it receives, based on the following:

Returns ‘fizz’ if the argument is divisible by 3
Returns buzz if the argument is divisible by 5
Returns ‘fizzBuzz’ if the argument is divisible by 3 and 5
Returns the argument itself if it is NOT divisible by 3 or 5
Returns exactly 'Invalid Argument' when an invalid argument is passed.

Examples: Input Output
3 "fizz"
5 "buzz"
15 "fizzBuzz"

i tried out this code but it didnt work

def fizz_buzz(n):
    n = []
    for nums in n:
        if nums % 5 == 0 and nums % 3 == 0:
            print("fizz buzz")
        elif nums % 3 == 0:
            print("fizz")
        elif nums % 5 == 0:
            print("buzz")
        elif nums % 3 != 0 and nums % 5 != 0:
            print(nums)
        else:
            print("invalid argument")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
benlegendj
  • 1
  • 1
  • 2
  • 2
    So how about writing that code now? – Mike Tung Jan 30 '18 at 18:27
  • You need to add code or at least show an attempt at adding code if you are looking for help. http://idownvotedbecau.se/noattempt/ – Zissouu Jan 30 '18 at 18:28
  • def fizz_buzz(n): n=[] for nums in n: if nums%5==0 and nums%3==0: print("fizz buzz") elif nums%3==0: print("fizz") elif nums%5==0: print("buzz") elif nums%3!=0 and nums%5!=0: print(nums) else: print("invalid arguement") – benlegendj Jan 30 '18 at 19:02
  • why are you overwritting what is being passed in with `n = []`? remove that line and it works. – MooingRawr Jan 30 '18 at 19:21
  • it is supposed to fetch the numbers from a list – benlegendj Jan 30 '18 at 19:47
  • the line `n = []`does NOT convert your input into a list. it overwrites it with an EMPTY list. So deleting that Line will help. `n` has to be a list even bevor you append the first value to it – sxeros Feb 26 '20 at 10:11

7 Answers7

1

easiest way I think:

def fizz_buzz(input):
    if (input % 3 == 0) and (input % 5 == 0):
        return 'FizzBuzz'
    if input % 3 == 0:
        return 'Fizz'
    if input % 5 == 0:
        return 'Buzz'
    return input

print(fizz_buzz(3))

PS: change print(fizz_buzz(3)) with any number to test it

0

here is one way you might do it ;P

from itertools import islice,count

targets = [3,5,15,22]
for target_value in targets:
    fizzbuzz = ("".join("BzuzzizF"[::2 * j] for j in (-1, 1) if 1 > i % (4 + j)) or i for i in count(1) )
    print(target_value,"=",next(islice(fizzbuzz,target_value-1,target_value+3)))
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

I wrote this to do it. It uses a dictionary to make it easy to modify it.

multiples = {3 : 'Fizz', 5 : 'Buzz'}
for i in range (1, 101):
    output = ''
    for j in multiples.keys():
        if i % j == 0:
            output += str(multiples.get(j))    
    if output == '':
        output = str(i)

    print (output)

If you change the dictionary to something like multiples = {3 : 'Fizz', 5 : 'Buzz', 7 : 'Fuzz'} it'll still work

0

Because of having to be a list I would do it somewhat like that:

def fizz_buzz(n):
    for nums in n:
        if nums % 5 == 0 and nums % 3 == 0:
            print("fizz buzz")
        elif nums % 3 == 0:
            print("fizz")
        elif nums % 5 == 0:
            print("buzz")
        elif nums % 3 != 0 and nums % 5 != 0:
            print(nums)
        else:
            print("invalid argument")


my_list = []

while True:
    user_input = input('Enter the Number: ')
    if user_input != 'exit':
         my_list.append(int(user_input))
    else:
        break

fizz_buzz(my_list)

In the Bottom part, I generate a list. You didn't provide any information about where you get your numbers from...so take that if you need it. But the fizz_buzz function now definitely works

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sxeros
  • 668
  • 6
  • 21
0

Here's your Code

def fizz_buzz(n):
   
  if (n % 3 == 0) and (n % 5 == 0):
    return 'FizzBuzz'
    
  if n % 3 == 0:
    return 'Fizz'

  if n % 5 == 0:
    return 'Buzz'

  return n

print(fizz_buzz(6))
Khalid Ali
  • 1,224
  • 1
  • 8
  • 12
0

here's mine

 def fizz_buzz(input):


    if (input % 3 == 0) and (input % 5 == 0):
        return "fizz_buzz"
    if input % 5 == 0:
        return "Buzz"
    if input % 3 == 0:
        return "fizz"
    return input

print(fizz_buzz(int(input("Enter your number:"))))

here's my answer it will return the same number if you input numbers that are not divisible to 3 and 5 and you can also input any number.

-1
def fizzbuzz(n):

    if n % 3 == 0 and n % 5 == 0:
        print('FizzBuzz')
    elif n % 3 == 0:
        print('Fizz')
    elif n % 5 == 0:
        print('Buzz')
    else:
        return str(n)

n=int(input("Enter number:"))

fizzbuzz(n)

this works...

sxeros
  • 668
  • 6
  • 21
  • 1
    As the OP states in his comment `n` has to be a list, so this doesn't work. You are missing the `Invalid Argument` case completely and instead of `return str(n)` better use `print(n)`. So your answer doesn't really answer the question. And pay attention to Code-Formating especially in Python!! – sxeros Feb 26 '20 at 10:08