Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LT_Forest Group Title

For the FizzBuzz program, I got only fizz and buzz and fizzbuzz for the output, but no numbers...What did I do wrong? I cant seem to figure out the issue here. Here is my code: for i in range(1,101): s = str(i) if i % 3==0 or i % 5==0: s='' if i % 3==0: s=s+'FIZZ' if i % 5==0: s = s+'BUZZ' print s

  • 10 months ago
  • 10 months ago

  • This Question is Closed
  1. ronin12 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    when you enter your first if loop you are resetting the value of s to just be a blank line which effectively is dropping out your number from the beginning of the line

    • 10 months ago
  2. LT_Forest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    what should I have instead of s=''?

    • 10 months ago
  3. ronin12 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    when your code hits s=str(i) the first time it sets s='1'. then you enter your if loop and have s='' which changes s='1' to s='' what your doing is called reinitializing a variable which is causing your original s to forget the '1'. btw the only reason im not outright telling you the fix is because problem solving/debugging is one of the most important skills for programming

    • 10 months ago
  4. LT_Forest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks I appreciate it...Id rather learn the process that just be told the answer any day. Let me tinker with this a little longer.

    • 10 months ago
  5. LT_Forest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    so here is what I went with and it worked like a charm. Thanks again for your help: for i in range (1, 101): if i % 3 == 0 and i % 5 == 0: print 'FIZZBUZZ' elif i % 3 == 0: print 'FIZZ' elif i % 5 == 0: print 'BUZZ' else: print i

    • 10 months ago
  6. ronin12 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    glad to see you got it worked out :D

    • 10 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.