27 lines
785 B
Markdown
27 lines
785 B
Markdown
def fizz_buzz(n):
|
|
"""
|
|
Print the FizzBuzz sequence from 1 to n.
|
|
For multiples of 3, print "Fizz" instead of the number.
|
|
For multiples of 5, print "Buzz" instead of the number.
|
|
For multiples of both 3 and 5, print "FizzBuzz".
|
|
"""
|
|
for i in range(1, n + 1):
|
|
if i % 15 == 0: # More efficient than checking i % 3 == 0 and i % 5 == 0
|
|
print("FizzBuzz")
|
|
elif i % 3 == 0:
|
|
print("Fizz")
|
|
elif i % 5 == 0:
|
|
print("Buzz")
|
|
else:
|
|
print(i)
|
|
|
|
def main():
|
|
"""Main function to handle input and call fizz_buzz."""
|
|
try:
|
|
n = int(input("Enter a number: "))
|
|
fizz_buzz(n)
|
|
except ValueError:
|
|
print("Please enter a valid integer.")
|
|
|
|
if __name__ == "__main__":
|
|
main() |