Python Python Basics All Together Now Cleaner Code Through Refactoring

Stefan Siljanovski
Stefan Siljanovski
1,255 Points

When asked how many tickets I input negative number (-5) and still sold me tickets ?

It still sold me tickets after answering negative number (-5) and gave me a total due of -48 and increased the number of tickets for variable tickets_remaining to 105 . How do we prevent that from happening ?

1 Answer

Steven Parker
Steven Parker
171,120 Points

The original code only checks if the requested number is more than the remaining tickets. But you can easily add another check for a number that is too small:

        if num_tickets < 1:
            raise ValueError("We cannot sell less than 1 ticket!")
Steven Parker
Steven Parker
171,120 Points

So based on your snapshot, those two lines could be placed between lines 15 and 16 or between 17 and 18.