How to fix 'TypeError' that comes up for some inputs

42 views Asked by At

I'm currently in year 10 and I'm creating a program which tells the user if their ransom note can be created from an article they have inputted. Some of the inputs I've been typing in come up with the error: TypeError: Can't convert 'NoneType' object to str implicitly

It seems to work at first but then I typed in "hello" as my ransom note and "hell" as my article and it came up with the error above. I thought it might've been because the article is shorter than the note but I tried it with other inputs and that doesn't seem to be the problem. I've included the function in case that might have something to do with it. Sorry if my code is a bit messy or inefficient.

elif choice == "2" :

    user_note = input("\nPlease enter your ransom note: ")

    user_article = input("Please enter your article: ")

    print("\n" + can_I_ransom(user_article, user_note))

can_I_ransom function:

def can_I_ransom(newspaper_text, ransom_text):
    article_list = list(newspaper_text)
    article_copy = list(newspaper_text)

    for i in range(len(ransom_text)):
        for j in range(len(article_list)):
            if ransom_text[i] == article_list[j]:
                del article_list[j]
                if len(article_copy)-len(ransom_text) == len(article_list):
                    return "Ransom can be made"
                break

        else:
            if j == len(article_list)-1:
                return "Ransom note cannot be made"

I'm expecting the output to be either "Ransom can be made" or "Ransom note cannot be made" and nothing else. Please help if you can :)

1

There are 1 answers

0
auden On

The problem is that when the ransom cannot be made, you aren't returning anything, and so it doesn't know what to do with a None which is what comes out when you just break without actually getting a "You can ransom" output. For example, what happens if the first if statement is true but the second isn't? Or if the first if statement is false and the second is false? This is why it only happens for some inputs - it only happens on the ones that slip through the cracks on the if statements. Also, I'm not quite sure your indentation is right for the outer else statement you have. Try running this:

def can_I_ransom(newspaper_text, ransom_text):
    article_list = list(newspaper_text)
    article_copy = list(newspaper_text)

    for i in range(len(ransom_text)):
        for j in range(len(article_list)):
            if ransom_text[i] == article_list[j]:
                del article_list[j]
                if len(article_copy)-len(ransom_text) == len(article_list):
                    return "Ransom can be made"
                else:
                    return "something"

            else:
                if j == len(article_list)-1:
                    return "Ransom note cannot be made"
                else:
                    return "something"
choice = "2"
if choice == "2" :

    user_note = input("\nPlease enter your ransom note: ")

    user_article = input("Please enter your article: ")

    print("\n" + can_I_ransom(user_article, user_note))

Just change the "something"s to the appropriate response.