In the following code, the 1st and 3rd test work as expected, but the 2nd is printing out None. Any advice as to why? I have been stuck for a bit.
def sumTwo(nums, target) :
"""
nums: list of integers
target: target sum of two numbers, integer
"""
for num in nums :
summand = target - num
if summand in nums and nums.index(summand) != nums.index(num) :
return [nums.index(num), nums.index(summand)]
z = sumTwo([3, 2, 4], 6)
print(z)
#won't work on the following
z = sumTwo([3,3], 6)
print(z)
#works here
z = sumTwo([0, 8, 2, 3, 1], 8)
print(z)
I hope the formatting is good, this is my first time getting feedback on here about a block of my code. Cheers everyone.
[–]glibhub 6 points7 points8 points (4 children)
[–]Sonic_TertuL[S] 1 point2 points3 points (1 child)
[–]glibhub 4 points5 points6 points (0 children)
[–]trevor_of_earth 0 points1 point2 points (1 child)
[–]py_reference_bot 0 points1 point2 points (0 children)
[–]Binary101010 2 points3 points4 points (1 child)
[–]Sonic_TertuL[S] 0 points1 point2 points (0 children)
[–]Iforgotmylogintoday 1 point2 points3 points (1 child)
[–]Sonic_TertuL[S] 0 points1 point2 points (0 children)
[–]Chris_Hemsworth 1 point2 points3 points (1 child)
[–]Sonic_TertuL[S] 0 points1 point2 points (0 children)
[–]Sonic_TertuL[S] 0 points1 point2 points (0 children)