-
-
Save jaysonrowe/1592775 to your computer and use it in GitHub Desktop.
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)) |
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)]
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)
for the fizz buzz game how to get the output for total number of fizz, buzz and fizzbuzz
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)
@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
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)]))
I got answer and also passed all test cases, by using ur single line code thanks!
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))
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
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
#!/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)
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)
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)
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)
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))
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()
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