0

Task is to : Write a function to check if one string is a substring of another, ignoring capitalisation.

I have this code so far but getting an error, any help would be great :

def is_substring(word, subword):
  if subword.lower() in word.lower():
    return True
  else:
    return False

c = is_substring('function', 'fun')
print (c)

This was my error :

Testing the first example in the question. Your submission did not produce the correct output. Your program output:

True
True
​
when it was meant to output:

True

Could anyone guide me to what is wrong with my code? Thanks.

4
  • 2
    Where are you running the code? Commented Sep 1, 2014 at 1:33
  • 2
    Can't reproduce in python3.4 Commented Sep 1, 2014 at 1:33
  • 2
    How is your function getting tested? If the caller is doing the printing, you should remove your own testing code (the last two lines) from the file. In general, you want top-level code in a Python file protected inside a if __name__ == '__main__': block so it isn't executed by other programs that import your script. Commented Sep 1, 2014 at 1:34
  • Yeah, it looks like the auto-grader runs your function and checks what's printed, don't print anything yourself or it might interfere with that. Commented Sep 1, 2014 at 1:35

2 Answers 2

3

It seems like you are sending this code to an auto-grader, I think it only expects the function definition (it may also need the call to the function). You can try these two ways:

  • Just providing the function definition:

    def is_substring(word, subword):
        if subword.lower() in word.lower():
            return True
        else:
            return False
    
  • Providing the function definition and the call:

    def is_substring(word, subword):
        if subword.lower() in word.lower():
            return True
        else:
            return False
    
    c = is_substring('function', 'fun')
    
Sign up to request clarification or add additional context in comments.

Comments

0

A more concise form of the function:

def is_substring(word, subword):
    return subword.lower() in word.lower()

It doesn't seem like you need that print statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.