Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Problem with try

from twitter import (
    tweet,
    MessageTooLongError,
    CommunicationError,
)


message = input("What would you like to tweet?  ")
# Your code here

def tweet(text):
    print(text)

tweet(message)

try:
    tweet(message)
except  CommunicationError:
        print("An error occurred attempting to connect to Twitter. Please try again!"

7 Answers

Python is very sinsitive about indentation. It seems like the most of your code uses 4 spaces as indentation but the print statement inside the except block is indented with 8 spaces

Thank you very much Sir!!! I’m going to try if it’s work

I tried Sir and put indentation of 4 spaces but still this problem on my running test:

FAIL: test_error_handled (main.TestFunctionWithCommunicationsErrorExecution)

Traceback (most recent call last): File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/unittest/mock.py", line 1179, in patched return func(*args, **keywargs) File "", line 18, in test_error_handled AssertionError: 'try again' not found in 'til that gushers are ravioli\ntil that gushers are ravioli' : Make sure you output the Try again message on CommunicationError

Just noticed that you're also missing a closing )

print("An error occurred attempting to connect to Twitter. Please try again!"  # <----

If this is not the problem then try to write the challenge description and your current code

from twitter import ( tweet, MessageTooLongError, CommunicationError, )

message = input("What would you like to tweet? ")

Your code here

def tweet(text): print(text)

tweet(message) try: tweet(message) except CommunicationError: print("An error occurred attempting to connect to Twitter. Please try again!")

I DON"T GET THIS MISTAKE

Traceback (most recent call last): File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/unittest/mock.py", line 1179, in patched return func(*args, **keywargs) File "", line 18, in test_error_handled AssertionError: 'try again' not found in 'til that gushers are ravioli\ntil that gushers are ravioli' : Make sure you output the Try again message on CommunicationError

The challenges are sometimes very sensitive to what you should print and what not to print.

From what I can see:

  • line 18
  • Make sure you output the Try again message on CommunicationError
try:
    # something here
except CommunicationError:
    print('Try again')

If this doesn't work then please share the challenge description or paste a link to the challenge

I appreciate your time and help but I guess there is some problem with the exercise. I did everything and still same error

Try share the challenge description or a link to the challenge

How is it work? I’m pretty new here so I don’t know.how the challenge works

answer is following,

from twitter import (
    tweet,
    MessageTooLongError,
    CommunicationError,
)


message = input("")

try:
    tweet(message)
except CommunicationError:
    print("An error occurred attempting to connect to Twitter. Please try again!")