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

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

Tickets second part of Project, 'Branch and Loop', 'while' loop function

i understand that "while" functions are like "if" statements. it checks if it is equal or not equal to what ever has been coded and what the user has been entered. When it comes down to non string numerical values assigned to a variable """tickets_remaining = 100""". Then involving the loop """while tickets_remaining:""". I don't understand how does the program know to stop the function loop if the ticket sells exceed 100? with a condition. Also later on in the video the teacher added a condition of """"while tickets_remaining >= 1:""" I don't understand this why greater then 1, why not less then 100? Also shouldn't the syntax be """ while tickets_remaining == >1:"""

here is a snapshot, so you can see clearly, click on file 'teachers_method.py'. https://w.trhou.se/ll8uzdu78n

If any can explain, I will kindly appreciate it, thanks.

3 Answers

Brett Miller
Brett Miller
3,663 Points

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help with syntax highlighting. (FYI the backtick on the US/English keyboard is left of the 1 key with "~" this symbol). For you question above check my comments below:

# The best way to solve most problems when you're starting out is to step through each line 
# of code keeping track of variables and other data.

# Your first time through this while loop cinema_tickets = 10 so cinema_tickets <= 10 is True and the loop runs
# In the loop, whatever is entered for num_tickets is subtracted from cinema_tickets in line 11
# If it is a positive amount, then cinema_tickets is less than 10 and the loop will run again.
# If you type a negative number in for num_tickets then cinema_tickets will be more than 10 and the loop will not run again
# This is different from the code I wrote above since your using "-=" rather than "+=" in line 11

cinema_tickets = 10

while cinema_tickets <= 10:

    print(cinema_tickets)

    num_tickets = int(input("how many cinema tickets would you like? "))
    should_proceed = input(" would you like to go ahead ? ")
    if should_proceed.lower() == "y":
        print("great tickets reserved !")
        cinema_tickets -= num_tickets
    else:
        print("no worries your welcome")

print("sorry no more tickets remaining ")

You should also try this practice course Practice Python While Loops

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Brett Miller, Hi, have you tested this code in shell/repl? As even when i exhaust the total number of cinema tickets the loop continues to run and does not print line 15. I can't get the code to print line 15. For future use, how did you get the code i made into the black box like you have?

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Brett Miller, Hi, have you tested this code in shell/repl? As even when i exhaust the total number of cinema tickets the loop continues to run and does not print line 15. I can't get the code to print line 15. For future use, how did you get the code i made into the black box like you have?

Brett Miller
Brett Miller
3,663 Points

Hello Mohan,

You're on the right track. To clarify, "if" statements and "while" loops check for boolean values (ie. True or False). Below are some examples:

while True:  # using a boolean value
while tickets_remaining == 100:  # equality operator (something equals something)
while tickets_remaining != 100:  # inequality operator (something does not equal something)
while tickets_remaining > 100:  # greater than operator (something is greater than something)
while tickets_remaining >= 100:  # greater than or equal operator (something is greater or equal to something)

# All of these evaluate to either True or False; If True it continues to loop; If False the loop is exited

So in the end of the video, the instructor uses "while tickets_remaining >= 1:" to make it clear that you should be able to purchase tickets only if there are one or more available but not if the number of tickets is zero or less.

Now when the instructor first wrote the "while" loop, they used:

while tickets_remaining:
   # do something

This syntax takes advantage of the truthyness or falseyness of values in python. As far a ints, 0 is evaluated as "False" and all other ints are "True". The above code will exit the "while" loop if the value of tickets_remaining equals 0. There is a flaw in this scenario though. If you have 1 ticket remaining and sell 2 or more, the program will continue to loop, which is why the instructor changed it to "tickets_remaining >= 1".

I hope this helps!

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Brett Miller Hi thanks for clearing that up, i came across another glitch that i cant explain, if i use the operator """ while tickets_remaining == 100:""". I.E the double equals sign and then the number 100 numercial value. When running it in repl or shell, for the first loop for any numerical value between 100 and under. It sell the tickets and then says its sold out even though there could be 20 remaining, 60 or 95 and so on.

https://w.trhou.se/uynn97t7v8

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Brett Miller Hi thanks for clearing that up, i came across another glitch that i cant explain, if i use the operator """ while tickets_remaining == 100:""". I.E the double equals sign and then the number 100 numercial value. When running it in repl or shell, for the first loop for any numerical value between 100 and under. It sell the tickets and then says its sold out even though there could be 20 remaining, 60 or 95 and so on.

https://w.trhou.se/uynn97t7v8

Brett Miller
Brett Miller
3,663 Points

The while loop executes the code inside of it as long as it evaluates to True.

# This loop will run once then exit
counter = 1

while counter == 1:
   # The code in here will run the first time, but the second time counter will not equal 1
    counter += 1


# This loop will run 10 times then exit
counter = 1

while counter <= 10:
   # The code in here will run 10 times
    counter += 1


# This loop will never run
counter = 1

while counter == 2:
   # This loop will never run since counter does not equal 2
    counter += 1

This is why the instructor used

while tickets_remaining >= 1:

so that it will continue to run until there are zero tickets remaining

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Brett Miller, thanks for the reply i think i sort of understand. The bottom code in your reply i dont get """counter = 1 (/n) while counter == 2: (/n) # This loop will never run since counter does not equal 2 (/n) counter += 1"""" Is it due to the fact that the variable """counter""" has been assigned the numerical value of 1 and the """while""" statement has been pegged to the value of 2. As the numerical value 1 and 2 are not equal it will not run? I tried testing your middle code here's a snapshot: https://w.trhou.se/1f21pv1u8b (file testwork.py) , when the tickets are sold out line 15 does not print/activate. (I undertand if the ""counter = 1 """ and the """while counter == 1:""" it will only print once, as with """while tickets_remaining == 100: """ if you enter 80 it exits the loop, 80 is not equal to a 100, theres 20 tickets left. where 1 is equal 1) Btw how do you insert those black boxes with code to make your life easier.

Brett Miller
Brett Miller
3,663 Points

My last post answers both of these questions. As your code is now, "cinema_tickets <= 10" will always evaluate to True if you are selling tickets so the loop will continue to run and never reach line 15. For the black code box, wrap your code with three backticks (```). Below the add an answer box there is a link that says Markdouwn Cheatsheet you should view to get more information.