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 Branch and Loop

problems with subtraction

Hi! I have compared my code against Craig's, but there are a couple of errors. I have only tinkered with this section of code,

            print("SOLD!")
            tickets_remaining -= num_tickets - tickets_remaining 

but I have gotten several different outputs, all of them wrong. currently my code looks like this:

TICKET_PRICE = 10

tickets_remaining = 100 

while tickets_remaining >= 1: 
    print("There are {} tickets remaining".format(tickets_remaining))
    name = input("What is your name?  ")  
    num_tickets = input("Hello, {}. How many tickets would you like? ".format(name))
    #expect a ValueError and handle it appropriately 
    try: 
        num_tickets = int(num_tickets)
        #raise value error if number of tickets requested is for more tickets than are available. 
        if num_tickets > tickets_remaining: 
            raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
        #Include the error text in the output 
        print("Oh no, we ran into an issue. {}. Please try again. ".format(err))
    else: 
        num_tickets = int(num_tickets) 
        amount_due = num_tickets * TICKET_PRICE
        print("The total due is ${}. ".format(amount_due)) 
        should_proceed = input("Do you want to proceed? Y/N?  ") 
        if should_proceed.lower() == "y": 
            #TODO: gather credit card info and process it. 
            print("SOLD!")
            tickets_remaining -= num_tickets - tickets_remaining 
        else: 
            print("Thank you, {} ".format(name))
print("Sorry the tickets are all sold out!!! :((  ") 

and it is spitting out

There are 100 tickets remaining                                                                                
What is your name?  Bella                                                                                  
Hello, Bella. How many tickets would you like? 3                                                           
The total due is $30.                                                                                          
Do you want to proceed? Y/N?  y                                                                                
SOLD!                                                                                                          
There are 197 tickets remaining                                                                                
What is your name?      

The problem here is that by saying there are 197 tickets left, it implies we started with 200, rather than 100.

What might be going wrong? Thanks for your help!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Isabella Elyse Letson Ettin, you’re using both in-place operator -= and the regular subtraction - operator.

# what your doing
>>> a = 100
>>> b = 3
>>> a -= b - a
>>> a
197
# this is equivalent to
>>> a = 100
>>> a = a - (b - a)
>>> a
197
# What you want is either
>>> a = 100 
>>> a -= b
>>> a
97
# or the equivalent long form
>>> a = 100 
>>> a = a - b
>>> a
97

Post back if you need more help. Good luck!!!