Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Details:
Example:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...
Variations (optional, for a more challenging exercise):
This question assesses a candidate's basic programming skills, understanding of loops and conditional statements, and ability to write clean, understandable code. The variations allow for exploring error handling, input validation, and more flexible program design.
This program implements the classic FizzBuzz problem, printing numbers from 1 to 100 (or a user-specified limit). Multiples of 3 are replaced with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
def fizzbuzz(limit=100, fizz_num=3, buzz_num=5, fizz_word="Fizz", buzz_word="Buzz"):
"""Prints FizzBuzz sequence up to the given limit.
Args:
limit (int): The upper limit of the sequence (inclusive).
fizz_num (int): The number to use for "Fizz" (default: 3).
buzz_num (int): The number to use for "Buzz" (default: 5).
fizz_word (str): The word to use for "Fizz" (default: "Fizz").
buzz_word (str): The word to use for "Buzz" (default: "Buzz").
"""
if not isinstance(limit, int) or limit <= 0:
print("Invalid limit. Please enter a positive integer.")
return
if not isinstance(fizz_num, int) or fizz_num <= 0:
print("Invalid Fizz number. Please enter a positive integer.")
return
if not isinstance(buzz_num, int) or buzz_num <= 0:
print("Invalid Buzz number. Please enter a positive integer.")
return
for i in range(1, limit + 1):
output = ""
if i % fizz_num == 0:
output += fizz_word
if i % buzz_num == 0:
output += buzz_word
if output:
print(output)
else:
print(i)
# Example usage with default values
fizzbuzz()
# Example usage with custom values
# fizzbuzz(limit=50, fizz_num=7, buzz_num=11, fizz_word="Foo", buzz_word="Bar")
fizzbuzz(limit=100, fizz_num=3, buzz_num=5, fizz_word="Fizz", buzz_word="Buzz")
Function:
limit
), the numbers for "Fizz" and "Buzz" (fizz_num
, buzz_num
), and the words to use (fizz_word
, buzz_word
) as input.limit
, fizz_num
, and buzz_num
are positive integers.limit
(inclusive).fizz_num
and/or buzz_num
.fizz_num
, it appends fizz_word
to the output
string.buzz_num
, it appends buzz_word
to the output
string.output
string is not empty (meaning the number is divisible by either fizz_num
or buzz_num
or both), it prints the output
string. Otherwise, it prints the number itself.Example Usage:
fizzbuzz()
: Calls the function with default values, printing the FizzBuzz sequence from 1 to 100 with 3 as "Fizz" and 5 as "Buzz".fizzbuzz(limit=50, fizz_num=7, buzz_num=11, fizz_word="Foo", buzz_word="Bar")
: (Commented out) Shows how to call the function with custom values to change the limit, numbers, and words.limit
, fizz_num
, and buzz_num
are expected to be positive integers. Input validation is included to handle cases where they are not.fizz_word
and buzz_word
are expected to be strings.limit
parameter allows the user to define the upper bound of the sequence.fizz_num
and buzz_num
parameters allow the user to change the divisors for "Fizz" and "Buzz".fizz_word
and buzz_word
parameters allow the user to customize the words used for "Fizz" and "Buzz".