我正在尝试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
问题的问题。
据我所知fizzbuzz
我code
分别在我自己的run
编辑器(pycharm)中分别正常工作。
但是当我在python
和class
行中添加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
为我期待它。
runs
和def
是否会在此处执行某些更改程序运行方式的内容?
答案 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>)