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 Python Basics All Together Now Handle Exceptions

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
231,269 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.