-5

I have this large script ( I will post the whole thing if I have to but it is very big) which starts off okay when I run it but it immediatly gives me 'TypeError: cannot concatenate 'str' and 'NoneType' objects' when it comes to this last bit of the code:

with open("self.txt", "a+") as f:
    f = open("self.txt", "a+")
    text = f.readlines()     
    text_model = markovify.Text(text)
    for i in range(1):
        tool = grammar_check.LanguageTool('en-GB')
        lin = (text_model.make_sentence(tries=800))
        word = ('' + lin)
        matches = tool.check (word)
        correct = grammar_check.correct (word, matches)
        print ">",
        print correct
        print ' '
        f = open("self.txt", "a+")
        f.write(correct + "\n")      

I have searched everywhere but gotten nowhere. It seems to have something to do with: word = ('' + lin). but no matter what I do I can't fix it. What am I doing wrong?

Barra
  • 111
  • 1
  • 7
  • 1
    Possible duplicate of [What is a 'NoneType' object?](https://stackoverflow.com/questions/21095654/what-is-a-nonetype-object) –  Nov 21 '17 at 00:36
  • You're trying to concatenate, `"string" + "string"` where one of the strings is in fact _not_ a string but a `NoneType`, possibly the one that is the return of a function. – Nae Nov 21 '17 at 00:38
  • 3
    It looks like `text_model.make_sentence()` can return `None`. You either have to fix _that_ function (so it always returns a string) or check for `None` in this code. –  Nov 21 '17 at 00:39
  • Python tracebacks show the line where the error occurred. – Michael Butscher Nov 21 '17 at 00:42
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 21 '17 at 00:50
  • @Blurp: How would I fix that function? Do I put it in 'str ()' to make it a string? (sorry I'm new to python.) – Barra Nov 21 '17 at 15:51
  • @MichaelButscher: Whats a traceback? – Barra Nov 21 '17 at 15:53
  • @Barra It depends on what `make_sentence()` does. Can you post it here? –  Nov 21 '17 at 17:33
  • @Barra Tracebacks are what you see when your program encounters an unexpected, unhandled error (i.e., an exception). ([docs](https://docs.python.org/3/tutorial/errors.html)) –  Nov 21 '17 at 17:55

2 Answers2

0

I'm not sure how I did it but with a bit of fiddling and google I came up with a solution, the corrected code is here (if you're interested):

with open("self.txt", "a+") as f:
                 f = open("self.txt", "a+")
                 text = f.readlines()     
                 text_model = markovify.Text(text)
                for i in range(1):
                  tool = grammar_check.LanguageTool ('en-GB')
                  lin = (text_model.make_sentence(tries=200))
                  matches = tool.check (lin)
                  correct = grammar_check.correct (lin, matches)
                  lowcor = (correct.lower())
                  print ">",
                  print str (lowcor)
                  print ' '
                  f = open("self.txt", "a+")
                  f.write(lowcor + "\n")      

Thanks for all the replies, they had me thinking and that's how I fixed it!

Barra
  • 111
  • 1
  • 7
-1

You can't concatenate a string and a NoneType object. In your code, it appears your variable lin is not getting assigned the value you think it is. You might try an if block that starts like this:

if type(lin) == str: some code else: raise Exception('lin is not the correct datatype')

to verify that lin is the correct datatype before printing.

kgmour
  • 1
  • 1
  • Thanks, this sort of worked. It got it printing lin but lin doesn't change (it's a markov chain string) when it's in the if statement so it just ends up printing the same thing. What do i do about that? – Barra Nov 21 '17 at 15:44
  • This is essentially what the original code does. It would be better to fix the underlying problem. –  Nov 21 '17 at 17:58