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.
mohan AbdulCourses 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.
Brett Miller3,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
Brett Miller3,663 Points
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!
Brett Miller3,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.