Skip to content

Instantly share code, notes, and snippets.

@jaysonrowe
Created January 11, 2012 03:05
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save jaysonrowe/1592775 to your computer and use it in GitHub Desktop.
Save jaysonrowe/1592775 to your computer and use it in GitHub Desktop.
FizzBuzz Python Solution
def fizzbuzz(n):
if n % 3 == 0 and n % 5 == 0:
return 'FizzBuzz'
elif n % 3 == 0:
return 'Fizz'
elif n % 5 == 0:
return 'Buzz'
else:
return str(n)
print "\n".join(fizzbuzz(n) for n in xrange(1, 21))
@roycho1988
Copy link

this is the code 👍
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
import sys

class MyWindow(QMainWindow):
def__init__(self):
super(MyWindow, self).def__init__()
self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Roy's Test")
self.initUI()

def initUI(self):
    self.label = QLabel(self)
    self.label.setText("My first Label")
    self.label.move(50, 50)

    self.b1 = QPushButton(self)
    self.b1.setText('click me')
    self.b1.clicked.connect(self.clicked)

def clicked(self):
    self.label.setText("yo pressed the QPushButton")
    self.update()

def update(self):
    self.label.adjustSize()

def window():
    app = QApplication(sys.argv)
    win = MyWindow()

    win.show()
    sys.exit(app.exec_())

window()

the error:
$ py test2.py
File "test2.py", line 7
def__init__(self):
^
SyntaxError: invalid syntax

@roycho1988
Copy link

illegal target for annotation python(parser-16) 3.7

@situnamrit
Copy link

x = range(1, 101)

for y in x:
if y % 3 == 0 and y % 5 == 0:
print ("Fizz Buzz")
elif y % 5 == 0:
print ("Buzz")
elif y % 3 == 0:
print ("Fizz")
else:
print y

@nielsly
Copy link

nielsly commented Apr 14, 2020

for i in range(100):print(i%3//2*'fizz'+i%5//4*'buzz'or i+1)

in 60 characters

edit:

for i in range(100):print(i%3//2*'fizz'+i%5//4*'buzz'or-~1)

in 59 characters

@AhmedOsman21
Copy link

I was trying to solve it by a function and It did work in this way:

def fizzBuzz(s):
    if s % 3 == 0 and s % 5 == 0:
        print("FizzBuzz")
    elif s % 3 == 0:
        print("Fizz")
    elif s % 5 == 0:
        print("Buzz")
    elif s % 3 == 0 and s % 5 == 0:
        print("FizzBuzz")
    else:
        print(s)


for i in range(1, 16):
    fizzBuzz(i)

I'm sorry if my code looks terrible that's because I'm new to programming.
hope it really helped.

@megactrl
Copy link

megactrl commented Sep 1, 2020

for fizzbuzz in range(16):
if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0:
print("fizzbuzz")
continue
elif fizzbuzz % 3 == 0:
print("fizz")
continue
elif fizzbuzz % 5 == 0:
print("buzz")
continue
print(fizzbuzz)

@Mr-Unchained
Copy link

def fizz_buzz(no):

if no %3 == 0:
    return 'fizz'
elif no %5 == 0:
    return 'buzz'
elif no %3 == 0 and no %5 == 0:
    return 'fizzbuzz'
else:
    return 'not_divisable'

no = int(input('enter no:'))
fizz_buzz(no)

@AonghusNaughton
Copy link

AonghusNaughton commented Nov 3, 2020

Hi, I am new to programming and I'm stuck on trying to make the results of the FizzBuzz game into a list.
I have done this but it only gives me back one string in the list and I can't think of any more ways to fix it. Please help.

for i in range(201):
a = []
if i % 3 == 0 and i % 5 == 0:
a.append('Fizzbuzz')
elif i % 3 == 0:
a.append('Fizz')
elif i % 5 == 0:
a.append('Buzz')
else:
a.append(str(i))
print(a)

@Mr-Unchained
Copy link

I'm new too, just started 2 weeks ago. Let's try changing your for loop to
For i in range (0, 201) and maybe add list to your print statement.

@AonghusNaughton
Copy link

Thank you for your help but I'm afraid it still just returns >> ['Buzz']

@Mr-Unchained
Copy link

def fizz_buzz(no):
if no %3 == 0:
return 'fizz'
elif no %5 == 0:
return 'buzz'
elif no %3 == 0 and no %5 == 0:
return 'fizzbuzz'
else:
return 'not_divisable'

enter = input('enter no:')
if enter.isdigit():
no = int(enter)

for i in range(0,no):
    a = fizz_buzz(i)
    print(a)

else:
print("not a number")

@AonghusNaughton
Copy link

a = []
for i in range(1, 201):
if i % 3 == 0 and i % 5 == 0:
a.append('Fizzbuzz')
elif i % 3 == 0:
a.append('Fizz')
elif i % 5 == 0:
a.append('Buzz')
else:
a.append(str(i))
print(a)

This give me exactly what I was looking for.

@mahnoor1279
Copy link

def fizzBuzz(n):
for i in range(1,n+1):
if i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)
inp=int(input())
output=fizzBuzz(inp)
output

@AruniMishra
Copy link

mylist = ["FizzBuxx" if x%3==0 and x%5==0 else "Fizz" if x%3==0 else "Buzz" if x%5==0 else x for x in range(1,101)]

@ankitrahul78
Copy link

def fizzBuzz(n):
for i in range(1,n+1):
if (i%15 == 0):
print("FizzBuzz")
elif (i%3 == 0):
print("Fizz")
elif (i%5 == 0):
print("Buzz")
else:
print(i)

@chaitan4350
Copy link

for the fizz buzz game how to get the output for total number of fizz, buzz and fizzbuzz

@big-dasher
Copy link

big-dasher commented Mar 2, 2021

Try this. It's only 2 lines of code.
for i in range(1, 101):
----print("Fizz" * (i%3<1) + (i%5<1) * "Buzz" or i)

@prajwalsm522
Copy link

@RobertAtomic
I am new to Python, why should it be range(1, 101)?

101 will not be taken ex:

range(1,5)
print(range)

output:
>>(1,2,3,4)

so last number will not be taken in python

@coder201-anjali
Copy link

print("\n".join(["Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(1,100)]))

Use this one , u get the real answer for the question.
print ("\n".join(["Fizz"(i%3==0)+"Buzz"(i%5==0) or str(i) for i in range(1,n+1)]))

@coder201-anjali
Copy link

I got answer and also passed all test cases, by using ur single line code thanks!

@sergiors
Copy link

sergiors commented Sep 28, 2021

def fizzbuzz(x):
    is_fizz = x % 3 == 0
    is_buzz = x % 5 == 0

    if is_fizz and is_buzz:
        return 'FizzBuzz'

    if is_fizz:
        return 'Fizz'

    if is_buzz:
        return 'Buzz'

    return x


r = map(fizzbuzz, range(1, 100))

print(list(r))

@Rabeet8
Copy link

Rabeet8 commented Oct 18, 2021

def fizzBuzz(n):
for i in range(1,16):
if i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('fizz')
elif i % 5 == 0 :
print('buzz')
else:
print(i)
The easiest way I could do

Copy link

ghost commented Nov 9, 2021

def FizzBuzz(numbersAndWords):
    for i in range(100):
        outString = ""
        for number in numbersAndWords.keys():
            if i % number == 0:
                outString += numbersAndWords[number]
        if outString == "":
            outString = i
        print(str(outString))

inGoes = {
        3:"Fizz",
        5:"Buzz"
        }

FizzBuzz(inGoes)

My preferred solution

@jonnadasairohit
Copy link

#!/bin/python3

import math
import os
import random
import re
import sys

Complete the 'fizzBuzz' function below.

The function accepts INTEGER n as parameter.

def fizzBuzz(n):

for x in list(range(1,n+1)):
output = ""
if(x % 3 == 0):
output += 'Fizz'
if(x % 5 == 0):
output += 'Buzz'
if(output == ""):
output += str(x)

print(output)
# Write your code here

if name == 'main':
n = int(input().strip())

fizzBuzz(n)

@davsciter
Copy link

In an interval of (1,N+1) the fuction will print Fizz if i value is divisible by 3, Buzz if is divisible by 5, and FizzBuzz if divisible by both. Else if none is true, it prints i.

def fizzbuzz(n):
   for i in range(1,n+1):
   txt=''

   if(x%3==0):
     txt+='Fizz'

   if(x%5==0):
     txt+='Buzz'

   print(txt) if len(txt)>0 else print(i)

   return

if __name__ = '__main__':
   n = int(input().strip())
   fizzbuzz(n)

@nntdesilva
Copy link

for n in range(1, 101):
    if not n % 3 and not n % 5:
        print('FizzBuzz')
    elif not n % 3:
        print('Fizz')
    elif not n % 5:
        print('Buzz')
    else:
        print(n)

Copy link

ghost commented Feb 5, 2022

1_R2fnmU0IU_YB5J2bz4_3hQ

@DaCuteRaccoon
Copy link

In 480 bits (60 bytes) or 60 chars:

for i in range(100):print(i%3//2*'Fizz'+i%5//4*'Buzz'or i+1)

@VanVictor
Copy link

def fizzBuzz(n):
# Write your code here
for i in range(1, n + 1):
if i % 5 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
elif i % 3 != 0 or i % 5 != 0:
print(str(i))

Proof, code at work

@connorjnel
Copy link

def fizzBuzz():
stack = range(1, 100, 1)
for item in stack:
if item % 3 == 0 and item % 5 == 0:
item = "FizzBuzz"
elif item % 3 == 0:
item = "Fizz"
elif item % 5 == 0:
item = "Buzz"

    print(item)

fizzBuzz()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment