Python Practice Creating and Using Functions in Python Practice Functions Use an External Function

Christopher Flores
Christopher Flores
6,898 Points

Where do I put the (42) ?

I know I am super close. I tried putting it in the tweet(message, 42), the MessageTooLongError (42) - nuthin' works

using_a_function.py
"""
This is importing a function named `tweet` from a file
    that we unfortunately don't have access to change.

You use it like so:
>>> tweet("Hello this is my tweet")

If the function cannot connect to Twitter,
    the function will raise a `CommunicationError`
If the message is too long,
    the function will raise a `MessageTooLongError`
"""
from twitter import (
    tweet,
    MessageTooLongError,
    CommunicationError,
)


message = input("What would you like to tweet?  ")
# Your code here
try:
    tweet(message)
except CommunicationError:
    print("An error occured attempting to connect to Twitter. Please try again!")
except MessageTooLongError as err:
    print("Oh no! Your message was too long (...)")

2 Answers

Josh Keenan
Josh Keenan
19,393 Points

You need to format the string!

# Your code here
try:
    tweet(message)
except CommunicationError:
    print("An error occurred attempting to connect to Twitter. Please try again!")
except MessageTooLongError as e:
    print(f'Oh no! Your message was too long {e}')

This prints out the string with the error formatted in.

Christopher Flores
Christopher Flores
6,898 Points

Thank you for your help. If you don't mind, can you explain to me the whole 'as e' 'print(f' and {e} thing? why would it not be 'as err' what is the point of the ' f ' ? I can see pass in the {e} because of 'as e' but how would it know 42 is the limit based on letters?

Josh Keenan
Josh Keenan
19,393 Points

It can be whatever you want, e, err, it's like a variable there is no need to name it anything in particular. the f is the latest version of python string formatting, it works the same as .format(), so it formats the error e into the string.