The question is to find any two distinctive numbers in the list that add up to 100, could anyone tell why my code is wrong, the output i'm getting is 50 and 50

values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15]
for x in values:
y=100-x
if y in values:
if x!=y:
value1=x
value2=y
break
break
print(value1)
print(value2)

Hi @amruth098

I’m sorry. But due to the lack of normal formatting, I can only guess what your code really looks like. And what exactly is your mistake.

You’re probably trying to do the following and this code is working.

values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15]
for x in values:
    y=100-x
    if y in values and x != y:
        value1=x
        value2=y
        break
print(value1)
print(value2)

Hi @amruth098, as @moriturus7 has pointed, without the normal formatting of your code it is difficult to know the output you are getting. Anyway, below code also gives you the expected output.

image

1 Like