【发布时间】:2020-11-04 07:46:16
【问题描述】:
这是一个来自 leetcode 的程序。当我当时在我的计算机上执行此代码时,它没有显示任何错误。但是当我在 leetcode 中运行相同的代码时,我开始得到这个错误以及输出。
class Solution:
def __init__(self ,nums,target):
self.nums = []
self.target = target
self.solution = []
def check():
for i in nums :
if -10**9 <= nums[i] <= 10**9 :
print("ok")
else :
sys.exit()
if -10**9 <= target <= 10**9 :
print("ok")
else :
sys.exit()
try :
def twosum(self):
if 2 <= len(nums) <= 10**5 :
for x in nums :
for y in nums :
if y != x and x+y == target :
solution = []
solution.append(nums.index(x))
solution.append(nums.index(y))
print("Because nums[" + str(nums.index(x)) + "] + nums[" + str(nums.index(y)) + "] == " + str(target) + ", we return" , solution)
return solution
break
else :
continue
except :
print("An exception occured")
nums = [1,2,3,4,5,6]
target = 4
demo = Solution(nums,target)
print(demo.twosum())
我得到了所需的输出:
Because nums[0] + nums[1] == 9, we return [0, 1]
[0, 1]
但我也遇到了错误:
TypeError: init() 缺少 2 个必需的位置参数:“nums”和“target” ret = Solution().twoSum(param_1, param_2) _driver (Solution.py) 中的第 62 行 _司机() (Solution.py) 中的第 73 行
【问题讨论】:
标签: python-3.x oop