MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / fizz_buzz

Function fizz_buzz

dynamic_programming/fizz_buzz.py:4–59  ·  view source on GitHub ↗

| Plays FizzBuzz. | Prints Fizz if number is a multiple of ``3``. | Prints Buzz if its a multiple of ``5``. | Prints FizzBuzz if its a multiple of both ``3`` and ``5`` or ``15``. | Else Prints The Number Itself. >>> fizz_buzz(1,7) '1 2 Fizz 4 Buzz Fizz 7 ' >>> fizz_

(number: int, iterations: int)

Source from the content-addressed store, hash-verified

2
3
4def fizz_buzz(number: int, iterations: int) -> str:
5 """
6 | Plays FizzBuzz.
7 | Prints Fizz if number is a multiple of ``3``.
8 | Prints Buzz if its a multiple of ``5``.
9 | Prints FizzBuzz if its a multiple of both ``3`` and ``5`` or ``15``.
10 | Else Prints The Number Itself.
11
12 >>> fizz_buzz(1,7)
13 '1 2 Fizz 4 Buzz Fizz 7 '
14 >>> fizz_buzz(1,0)
15 Traceback (most recent call last):
16 ...
17 ValueError: Iterations must be done more than 0 times to play FizzBuzz
18 >>> fizz_buzz(-5,5)
19 Traceback (most recent call last):
20 ...
21 ValueError: starting number must be
22 and integer and be more than 0
23 >>> fizz_buzz(10,-5)
24 Traceback (most recent call last):
25 ...
26 ValueError: Iterations must be done more than 0 times to play FizzBuzz
27 >>> fizz_buzz(1.5,5)
28 Traceback (most recent call last):
29 ...
30 ValueError: starting number must be
31 and integer and be more than 0
32 >>> fizz_buzz(1,5.5)
33 Traceback (most recent call last):
34 ...
35 ValueError: iterations must be defined as integers
36 """
37 if not isinstance(iterations, int):
38 raise ValueError("iterations must be defined as integers")
39 if not isinstance(number, int) or not number >= 1:
40 raise ValueError(
41 """starting number must be
42 and integer and be more than 0"""
43 )
44 if not iterations >= 1:
45 raise ValueError("Iterations must be done more than 0 times to play FizzBuzz")
46
47 out = ""
48 while number <= iterations:
49 if number % 3 == 0:
50 out += "Fizz"
51 if number % 5 == 0:
52 out += "Buzz"
53 if 0 not in (number % 3, number % 5):
54 out += str(number)
55
56 # print(out)
57 number += 1
58 out += " "
59 return out
60
61

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected