Python Python Basics All Together Now Handle Exceptions

Anthony Grodowski
Anthony Grodowski
4,891 Points

Code not raising ValueError

https://w.trhou.se/ljqzn8k9gs Line 15 code doesn't work while putting larger number of tickets than available. What did I wrong? Also my code doesn't update tickets remaining value - it shows 100 all the time even if it should say that they're all sold out. TIA

1 Answer

adrian miranda
adrian miranda
11,444 Points

This has a number of issues. First, in line 6, you say

tickets_remaining = (ticket_remaining - ticket_number)

Mostly in this script you refer to tickets_remaining, but on this line you also have ticket_remaining. I think you just typoed, and left the s out of tickets.

Also, the first time through this loop, ticket_number isn't even set. You shouldn't be subtracting it there. Try doing it right after you say SOLD. (Think about it. If someone said no, they didn't want to buy it, you would still do the subtraction.)

Line 6 should really throw an error in a modern python, it's odd that you aren't getting one.

Also, when you say:

        raise ValueError("There are only {} tickets remaining".format(ticket_number))

I think you mean to use tickets_remaining, not ticket_number

Good luck!

Anthony Grodowski
Anthony Grodowski
4,891 Points

Thank you Adrian! I've made a couple of changes but now I face a problem, which appeard while trying to improve my code. (https://w.trhou.se/tc2bkyh1ty)

That's how program is running while putting too large number of tickets:

"Welcome to our aplication! What's your name? Anthony
Hello, Anthony!
How many tickets would you like to buy? 900
Oh no! There's an issue! There are only 900 tickets remaining. Please try again
There are 100 tickets remaining."

As you can see it puts ticket_number in the sentence telling how many tickets are remaining instead of tickets_remaining. What have I done wrong?