Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fizz Buzz Example
#1
Just a simple Fizz Buzz example answer that you guys can dissect and point things out.  Smile 
count = 0

while count <= 100:
        
 if ((count % 5) == 0 and (count % 3) == 0):
     print "FizzBuzz"
     count = count + 1
    
 elif (count % 3) == 0:
        print "Fizz"
        count = count + 1
        
 elif (count % 5) == 0:
        print "Buzz"
        count = count + 1
    
 else:
        print count
        count = count + 1
Reply
#2
By using a for loop you remove the need for the count number. In fact, if this actually were for a job interview, whether or not an applicant used a while or a for would be quite telling of Python knowledge. Also there is no need to have a conditional for both parts being true.

A sane version of fizz buzz could look like this:
for i in range(1,101):
    line = ""
    if i%3 == 0:
        line = "Fizz"
    if i%5 == 0:
        line += "Buzz"
    print(line or i)
That said, I quite like sillier versions like this:
for i in range(1,101):
    print("Fizz" * (i%3==0) + "Buzz" * (i%5==0) or i)
There is also a slightly shorter version than that but it isn't as clear.
Rosetta Code is a fun site if you don't know about it:
https://www.rosettacode.org/wiki/FizzBuzz#Python
Reply
#3
Wow @Mekire that is way above my pay grade, I will study this, thank you for the feedback!
The K Man.
Reply
#4
(Nov-07-2016, 04:13 PM)Mekire Wrote: In fact, if this actually were for a job interview, whether or not an applicant used a while or a for would be quite telling of Python knowledge.
Almost any programming experience, for that matter.  I actually can't think of a language where you'd need to use a while loop for this.  Even php has for loop syntax for this sort of thing:
for ($i=0; $i < 101; $i++) {  /* fizzbuzz math */ }
Reply
#5
(Nov-07-2016, 04:31 PM)nilamo Wrote:
(Nov-07-2016, 04:13 PM)Mekire Wrote: In fact, if this actually were for a job interview, whether or not an applicant used a while or a for would be quite telling of Python knowledge.
Almost any programming experience, for that matter.  I actually can't think of a language where you'd need to use a while loop for this.  Even php has for loop syntax for this sort of thing:
for ($i=0; $i < 101; $i++) {  /* fizzbuzz math */ }

try it in assembly Big Grin
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Nov-07-2016, 04:13 PM)Mekire Wrote: By using a for loop you remove the need for the count number.  In fact, if this actually were for a job interview, whether or not an applicant used a while or a for would be quite telling of Python knowledge.  Also there is no need to have a conditional for both parts being true.

A sane version of fizz buzz could look like this:
for i in range(1,101):
    line = ""
    if i%3 == 0:
        line = "Fizz"
    if i%5 == 0:
        line += "Buzz"
    print(line or i)
That said, I quite like sillier versions like this:  
for i in range(1,101):
    print("Fizz" * (i%3==0) + "Buzz" * (i%5==0) or i)
There is also a slightly shorter version than that but it isn't as clear.
Rosetta Code is a fun site if you don't know about it:  
https://www.rosettacode.org/wiki/FizzBuzz#Python
Gotta love rosetta lol.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020