def如何改变python函数

时间:2017-05-17 04:40:17

标签: python function class

我正在尝试class Jobs extends Model { public function candidate() { return $this->hasMany(Candidate::class,'applied_job_id'); } } class Candidate extends Model { } $jobs = Jobs::withCount('candidate')->limit($this->limit)->offset($offset)->get(); 上的一些练习问题,而我遇到了leetcode问题的问题。

据我所知fizzbuzzcode分别在我自己的run编辑器(pycharm)中分别正常工作。

但是当我在pythonclass行中添加def时,只需返回完成code代码0的流程。

exit

当我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)) 在网站上检查答案时,只返回run但如果我将['1']class行取出def为我期待它。

runsdef是否会在此处执行某些更改程序运行方式的内容?

3 个答案:

答案 0 :(得分:1)

def用于创建函数,需要调用函数才能执行它们

答案 1 :(得分:0)

您需要在示例中运行此代码!

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)

答案 2 :(得分: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>)