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

Isabella Elyse Letson Ettin
Isabella Elyse Letson Ettin
513 Points

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 67,778 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!!!