Code Monkey home page Code Monkey logo

s-shemmee / fizzbuzz-challenge-python Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 4 KB

This is a programming challenge where your goal is to write a program that prints the numbers from 1 to 100. But for multiples of 3 print "Fizz" instead of the number, and for the multiples of 5 print "Buzz". For numbers that are multiples of both 3 and 5 print "FizzBuzz".

License: MIT License

Python 100.00%
fizz-buzz fizzbuzz fizzbuzz-python fizzbuzz-solution

fizzbuzz-challenge-python's Introduction

The Famous Fizzbuzz Challenge

Challenge time! You are going to use what you've learned so far to solve the famous FizzBuzz Coding Challenge. The challenge is one of the most common coding interview questions.

Here's the FizzBuzz challenge.

  • Create a new file called fizzbuzz.py:
# 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 that are multiples of both three and five print "FizzBuzz".

# Tip: % (modulo) tells you what's left over when you divide one number by another
# ex. number % 3 == 0

Good luck!

FizzBuzz Challenge: Solution

Here's the answer to FizzBuzz in Python. Hopefully, you gave the FizzBuzz challenge a try. Whether you got the solution or not, let's work through the logic together.

Step #1

  • First print out the numbers from 1 to 100:
for number in range(1,101):
    print(number)

Step #2

  • Print "Fizz" if the number is divisible by 3:
for number in range(1,101):
    if number % 3 == 0:
        print("Fizz")
    else:
        print(number)

Step #3

  • Print "Buzz" if the number is divisible by 5:
for number in range(1,101):
    if number % 3 == 0:
        print("Fizz")
    elif number % 5 == 0:
        print("Buzz")
    else:
        print(number)

Step #4

  • Print "FizzBuzz" if the number is divisible by 3 and 5:
for number in range(1,101):
    if number % 3 == 0 and number % 5 == 0:
        print("FizzBuzz")
    elif number % 3 == 0:
        print("Fizz")
    elif number % 5 == 0:
        print("Buzz")
    else:
        print(number)

(Note that this new condition number % 3 == 0 and number % 5 == 0 has to go above the other two because otherwise the others run first.)

fizzbuzz-challenge-python's People

Contributors

s-shemmee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.