mikeizbicki / cmc-csci040

Computing for the Web
37 stars 58 forks source link

Project 4 issue #297

Closed chasekeir closed 1 year ago

chasekeir commented 1 year ago

Hi Mike,

I am running my code for task #4 in bot.py:

if len(comments_without_replies)>0:

                random_comment = random.choice(comments_without_replies)
                random_comment.reply(body=generate_comment())
                print('reply to a comment')

for context here is my code for task #3

comments_without_replies = []
            for comment in not_my_comments:
                replied = True
                for comment.reply in not_my_comments: 
                    if comment.author == 'botfly123':
                        replied = True
                        break 
                    if comment.author != 'botfly123':
                        replied = False 
                if replied:
                    continue
                else:
                    comments_without_replies.append(comment)

Here is the error message I am receiving:

new iteration at: 2022-11-22 20:16:50.724450
submission.title= My wife is a poll worker has this report
submission.url= https://www.reddit.com/r/cs40_2022fall/comments/yz64qm/my_wife_is_a_poll_worker_has_this_report/
len(all_comments)= 11
len(not_my_comments)= 10
len(comments_without_replies)= 10
Traceback (most recent call last):
  File "/Users/chasekeir/CSCI040/bot.py", line 215, in <module>
    random_comment.reply(body=generate_comment())
TypeError: 'Comment' object is not callable

How can I fix this?

mikeizbicki commented 1 year ago

First, I'll just say that you asked this question in a fantastic way. You gave me the entire error message and all the relevant pieces of code, and you formatted everything properly with code blocks. This makes it very easy for me to find the problem and help.

The key here is the exception you are getting

TypeError: 'Comment' object is not callable

indicates that you are putting parentheses next to something that's not a function. (We covered this in the exceptions quiz if you need to go back and review exactly why that's the error.)

The line causing this error is

    random_comment.reply(body=generate_comment())

and there's exactly two uses of () here: one on random_comment.reply and the other on generate_comment. One of these two things must not actually be a function, but a Comment object.

I agree that both of these things "should" be a function, but somewhere you must have redefined one of them to be something else. To see exactly which one is causing the problem, you can separate it out into two lines like

    text = generate_comment()
    random_comment.reply(body=text)

My guess is that you'll find that the error now happens on the first line and not the second. This would most likely be caused by a line somewhere else in your code that redefines the generate_comment function to be something else. This likely looks something like

generate_comment = ....

and you'll have to find and fix the above line.