r/pythonhelp • • Jun 06 '22

FizzBuzz Problem

Hello,

I am struggling with a practice FrizzBuzz problem.

def StringChallenge(num):
    for i in range (1, num+1):
        if(i % 5 == 0 and i % 3 == 0):
            print("FizzBuzz", end = ' ')
        elif(i % 5 == 0):
            print("Buzz", end = ' ')
        elif(i % 3 == 0):
            print("Fizz", end = ' ')
        else:
            print(i, end = ' ')
            
    return num
print(StringChallenge(input()))

I am trying to make it work so it all outputs on the same line. The first line and last two lines are required.

0 Upvotes

5 comments sorted by

1

u/MT1961 Jun 06 '22

Well, the only thing I see wrong with it is that you are passing a string (input() returns a string) to the function. Modify it to pass int(input()) and it should do what you want. Presuming of course, you want Fizz for evenly divisible by 3, Buzz for evenly divisible by 5 and FizzBuff for evenly divisible by 15.

1

u/jewniah Jun 06 '22

That works, except that I cannot edit the last line of code, it's forced on me. I tried deleting the return line to see what happens and it prints None instead of the last integer. Would this be the only way to have it fixed? I guess I could break the rules if the result is correct lol

3

u/MT1961 Jun 06 '22

I see. Okay, so change your function to read like this:

def StringChallenge(snum):

and then add this line to the top of it:

num = int(snum)

1

u/AmongstYou666 Jun 07 '22

  
def  StringChallenge(num):
    for  i  in  range  (1,  num+1):
        if (not i%5 and not i%3):
            print("FizzBuzz",  end  =  '  ')
        elif (not i%5):
            print("Buzz",  end  =  '  ')
        elif (not i%3):
            print("Fizz",  end  =  '  ')
        else:
            print(i,  end  =  '  ')
                        
StringChallenge(int(input('number  ')))
print()
 

0

u/[deleted] Jun 06 '22

[deleted]

1

u/jewniah Jun 06 '22

Hi, thank you for the reply. The only issue I have is that the last two lines are required, so I cannot edit it as you did :/. Is there another way around this? I'm trying to do it, but it ends up outputting something like:

1 2 3 Fizz