Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Handle Exceptions

caitlin oloughlin
caitlin oloughlin
3,099 Points

Getting a different exception text than the one I'm trying to raise

Hi, I completed the project (a little differently than the video, but it works). However, one issue I'm having is that when someone inputs something besides an integer I get an error that reads "invalid literal for int() with base 10 instead of the one I'm trying to raise below. Any advice on how to change that? thanks!

import sys SERVICE_CHARGE = 2 TICKET_PRICE = 10 tickets_remaining = 100 total_sold = 0

def calculate_price(total_sold): return total_sold * TICKET_PRICE

while tickets_remaining >= 0: name = input("What's your name? ") try: total_sold = int(input(f"Hi {name}! How many would you like?: ")) if total_sold > tickets_remaining: raise ValueError("not enough tickets remaining")

except ValueError as err:
    print("Oh no! we ran into an issue")
    print(f"{err}")
    # confirms if they want to purchase
else:
    total_cost = calculate_price(total_sold) + SERVICE_CHARGE
    tickets_remaining -= total_sold
    confirm = input(f"Are you sure you want to purchase {total_sold} tickets for ${total_cost}? ")
    confirm = confirm.lower()

    if confirm == "yes":
        print(f"Congrats! you just bought {total_sold} tickets. There are {tickets_remaining} left.")
    # if they say no
    else:
        print("Ok, we hope you change your mind!")

if they're sold out

else: sys.exit("Oh no! We're sold out")

1 Answer

Steven Parker
Steven Parker
217,531 Points

You should first see: "Oh no! we ran into an issue", and then the next line is the system error message printed out by the print(f"{err}") command. The message you are seeing is the correct system-level text for the error raised when the int() function tries to convert something that is not a number (in base 10). This is the same thing that happens in the video when the instructor answers the quantity question with the word 'blue'. What did you expect to see instead?

And for future questions, be sure to use Markdown formatting to preserve the code's appearance and retain special symbols. An even better way to share code and make your issue easy to replicate is to make a snapshot of your workspace and post the link to it here.