-1

I am trying some of the practice problems on leetcode and I am running into an issue on the fizzbuzz problem.

As far as I can tell my code works correctly when I run it separately in my own python editor (pycharm) .

But when I add back in the class and def lines running the code just comes back with process finished with exit code 0.

class Solution(object):
    def fizzBuzz(self, n):

        ans = []
        for i in range(1, 6):
            if (i % 3 == 0) and (i%5 != 0):
                ans.append("fizz")
            if (i% 3 != 0) and (i%5 == 0):
                ans.append("buzz")
            if (i % 3 != 0) and (i % 5 != 0):
                ans.append(str(i))

When I run this on the websites check answer thing only get back ['1'] but if I take out the class and def lines it runs as I expected it to.

Whether def and class do something here that changes how the program runs?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Taylor. C
  • 1
  • 4
  • Please correct your indentation – kuro May 17 '17 at 04:44
  • If you define a function with `def`, you need to call it somewhere in your program, otherwise the code inside the function will not be run. Also, if the function is a class method, you will have to create a class object and call its member function after that. – Thomas Kühn May 17 '17 at 04:47
  • I was assuming that in this the run code button calls the class and function itself. As far as I can tell it does run the code and call the function since when I give it custom input of 5 so its not just testing 1 I only get back 1. I edited the code portion to take out a bunch of extra fluff that I had in there for debugging purposes. @thomas kuhn it might be a duplicate, im not sure if the issue im having is because there is something wrong with my code of if im assuming too much automation in leetcode's "run code" button. – Taylor. C May 17 '17 at 04:50
  • @Taylor.C, if you add `class` and `def` the output is `[1]`? – kuro May 17 '17 at 04:55
  • if I add the class and def lines in pycharm it just exits with a code of 0 which makes sense since im running a object with no call. when I run it without the def and class lines, so just testing the code inside I get the right output but with the def and class lines added back in I only get 1 when submitting to leetcode, so im unsure if im doing something wrong. – Taylor. C May 17 '17 at 04:56

3 Answers3

1

def is used to create a function, you need to call functions in order to execute them

Dat Boi
  • 115
  • 1
  • 7
0

You need to run this code how in example!

class Solution(object):
    def fizzBuzz(self, n):

        ans = []
        for i in range(1, 6):

            # app = False
            print(i)
            if (i % 3 == 0) and (i%5 != 0):
                ans.append("fizz")
                print("appended fizz")
             #  app = True
            if (i% 3 != 0) and (i%5 == 0):
                ans.append("buzz")
                print("appended buzz")
             #   app = True
            if (i % 3 != 0) and (i % 5 != 0):
                ans.append(str(i))
                print("appended i")
            app = False
        print("final answer")
        print(ans)

if __name__ == '__main__':
    solution = Solution()
    n = input('Please, input n: ')
    solution.fizzBuzz(n)
Hanz
  • 16
  • 1
  • That works for running it myself which does solve half of my problem. I guess my main issue is that I dont understand how much leetcode automates checking code when you submit your answer. – Taylor. C May 17 '17 at 05:02
0
#Seems like you are new to Object oriented programming in python,
#keyword def is used to define a method and key word class for 
#defining a class. Since you have defined fizzbuzz inside the class
#Solution, you need to create an object for that class to access its members
class Solution(object):
    def fizzBuzz(self, n):

        ans = []
        for i in range(1, 6):
            if (i % 3 == 0) and (i%5 != 0):
                ans.append("fizz")
            if (i% 3 != 0) and (i%5 == 0):
                ans.append("buzz")
            if (i % 3 != 0) and (i % 5 != 0):
                ans.append(str(i))

solutionObj = Solution()
solutionObj.fizzBuzz(<your input>)
Amey Kumar Samala
  • 904
  • 1
  • 7
  • 20