It's a function that adds two different elements from the list and returns 1 if the value becomes K, otherwise returns -1 No matter what you put in K, only -1 will be returned. Which part should I fix to make it work?
def doublek(myl,k):
for i in range(len(myl)):
for j in range(len(myl)-1):
if myl[i]+myl[j]==K:
return(1)
else:
return(-1)
myl=[1,3,5,7,9,11,21]
print(doublek(myl,K)). #If you put anything in K, you get -1.
I don't know if the indentation was done properly because I wrote it on my phone, but I put it in the right indentation in the actual code, so I'd appreciate it if you could tell me where to fix it to work properly.
loops
if myl[i]+myl[j]==K:
--> if myl[i]+myl[j]==k:
You just made the wrong code.
def doublek(myl,K):
for i in range(len(myl)):
# # i = 0
for j in range(len(myl)-1):
# # j = 0
if myl[i]+myl[j]==K:
# If k is not 2 because it is 1+1=2, return (-1) and terminate the function
return(1)
else:
return(-1)
myl=[1,3,5,7,9,11,21]
print(doublek(myl,K))
© 2024 OneMinuteCode. All rights reserved.