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

Help with Raise ValueError in Python

money_needed = 500

while money_needed > 1:
    print("We need {} dollars.".format(money_needed))
    name = input("Hello, may I have your name? ")
    answer = input("{}, would you like to help plan Maya's birthday? yes/no? ".format(name))
    if answer.lower() == 'yes':
        try:
            contribution = float(input("How much would you like to contribute? "))
            money_needed = money_needed - contribution
            if contribution < 1:
                raise ValueError("Please donate $1 or more. Thank you.")
        except ValueError as err:
            print("Oh no. Not valid. Try again.")
    else:
        print("No worries. I hope you have a great day.")

Why is my message is not printing when I raise the ValueError ("Please donate $1 or more. Thank you")

1 Answer

Steven Parker
Steven Parker
231,269 Points

Passing the message as an argument to the raise makes it available to the handler, but you still need to print it out in the handler. Either by itself:

    print(err)

Or, as part of a larger message:

    print("Oh no. Not valid. " + str(err) + "  Try again.")