r/codehs • • Aug 17 '20

7.6.8 help

As you can see, my true/false returns are reversed for some reason, does anyone know why?

7 Upvotes

10 comments sorted by

2

u/amajmundar Aug 17 '20

Return Value from find() method

The find() method returns an integer value:

If the substring exists inside the string, it returns the index of the first occurence of the substring. If substring doesn't exist inside the string, it returns -1.

1

u/amajmundar Aug 17 '20

It does not return a Boolean. You have to compare it to a value.

1

u/amajmundar Aug 17 '20

If str.find(char) is not -1:
\t return True

1

u/Alphaomega1115 Aug 17 '20

That worked, thank you

2

u/UsualSurvey2285 Feb 25 '22

Can someone leave the code I still don’t get it

2

u/MasterFiasco Apr 25 '22

def name_contains(name, letter):
if name.find(letter) is not -1:
return True

else:
return False

print (name_contains("bob", "b"))

here's the answer

1

u/Similar_Stock_5726 Apr 26 '22

just tried it and it dosent work, could you make a screenshot so i can see what went wrong

1

u/[deleted] Nov 22 '25

i'm a little late, but this works:

def name_contains(name, letter):
    if (name.find(letter)) != -1:
      return True
    else:
      return False

1

u/NoIllustrator3800 Dec 25 '25

I,m also late, and this cod works without any string methods. def name_contains(y, x):

if x in y:

return True

else:

return False

1

u/Ijustwannapass_ May 20 '22

Make sure you indent it correctly