-5

My fizzbuzz code keeps giving me none as an output i dont know why please help.

Here is my code:

def FizzBuzz(num):
  for num in range (1,50):
   string = ""
  if num % 3 == 0:
     string = string + "Fizz"
  if num % 5 == 0:
     string = string + "Buzz"
  if num % 5 != 0 and  num % 3 != 0:
    string = string + str(num)
    print (string)
 
print FizzBuzz(raw_input())
LoveBugg
  • 1
  • 1
  • 1
    Could it be because the indentation of all the if statements are wrong? They should be part of the for loop body. Also, the last print should not be inside an if statement at all. The reason you're getting None is because FizzBuzz doesn't return anything, so printing its output prints what it returns: None. – B Remmelzwaal Apr 28 '23 at 15:34
  • 1
    You seem to expect `FizzBuzz` to return something; where in your code do you think you make that happen? – Scott Hunter Apr 28 '23 at 15:35
  • 1
    Also, avoid CamelCase as function's name, they usually are used for class name. – Itération 122442 Apr 28 '23 at 15:35
  • Maybe you wanted to write `def FizzBuzz(max_num): for num in range(1, max_num + 1) :` – Jorge Luis Apr 28 '23 at 15:54
  • 1
    Remember `raw_string` returns a `str`, you might want to convert it into `int`. – Jorge Luis Apr 28 '23 at 15:55
  • If you're using `raw_input()` it means you're still using Python 2.x, which has been EOL for several years. You should upgrade to 3.x. – Barmar Apr 28 '23 at 16:11
  • Can you write it for me so i can see please – LoveBugg Apr 28 '23 at 16:11
  • 3
    There are many fizzbuzz questions here, you can simply search for them. – Barmar Apr 28 '23 at 16:11

1 Answers1

0

You have not used proper intendation in the code.

   print FizzBuzz(raw_input())

This statement doesn't print anything because the FizzBuzz function is not returning any value to the function call. (None is taken as return value)

def FizzBuzz(num):
    for num in range (1,50):
        string = ""
        if num % 3 == 0:
            string = string + "Fizz"
        if num % 5 == 0:
            string = string + "Buzz"
        if num % 5 != 0 and  num % 3 != 0:
            string = string + str(num)
        print (string)
FizzBuzz(raw_input()) # function call (no need to use print statement as nothing is returned```