Python Challenge - FizzBuzz, FizzBuzz

Nice one. Given that msg is dedicated to the function, I would write it inside to keep it out the global namespace, safely tucked away.

>>> def fizzbuzz(limit):
    msg = { 6: "Fizz", 10: "Buzz", 0: "FizzBuzz" }
    return [msg.get(i ** 4 % 15, i) for i in range(1, limit + 1)]

>>> 
1 Like
def fizzbuzz(limit): # Write your code here new_list = [] working_list = range(1, (limit+1)) for n in working_list: if (n % 3 == 0 and n % 5 == 0): new_list.append('FizzBuzz') elif n % 5 == 0: new_list.append('Buzz') elif n % 3 == 0: new_list.append('Fizz') else: new_list.append(n) return new_list print(fizzbuzz(30))

Welcome to the forums! Looks great, your code gives the right output! There is a way to make it more concise though, and it involves doing away with working_list as a variable. How might you simplify your code?

def fizzbuzz(limit): # Write your code here fizzbuzzlist = [] for i in range(1, limit+1): if i % 3 == 0 and i % 5 == 0: fizzbuzzlist.append("FizzBuzz") elif i % 3 == 0: fizzbuzzlist.append("Fizz") elif i % 5 == 0: fizzbuzzlist.append("Buzz") else: fizzbuzzlist.append(i) return fizzbuzzlist print(fizzbuzz(16))
def fizzbuzz(limit): # Write your code here x=[] for i in range(1,limit+1): if i%3==0 and i%5==0: x.append("FizzBuzz") elif i%3==0: x.append("Fizz") elif i%5==0: x.append("Buzz") else: x.append(i) return x print(fizzbuzz(16))