Reputation: 11
Very recently started to learn Python (2 weeks ago) and have been enjoying working through the Python Crash Course book. I've written some code for an exercise in the book. The code works as I expected it to but I don't quite understand why.
Exercise: Write a program for a cinema that asks the user for inputs of their age and then tells them what price their ticket will be, using a while loop.
Here is the code I first wrote:
print("Welcome to Cathrine's Cinema!\n")
no_tickets = 0
total_cost = 0
while True:
if no_tickets == 0:
ticket = input("Would you like to buy a ticket? (Yes/No) ")
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print("Ok, thanks for coming by!")
break
else:
print("Please answer 'Yes' or 'No'")
continue
if no_tickets > 0:
ticket = input("Would you like to buy another ticket? (Yes/No) ")
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print(f"Cool, you have purchased {no_tickets} tickets for a total"
f" cost of £{total_cost}.")
break
else:
print("Please answer 'Yes' or 'No'")
continue
I thought it was quite awkward having two big almost identical if statements just so the initial message (Would you like to buy a/another ...) and the goodbye message was right, so I wrote it again a bit differently.
print("Welcome to Cathrine's Cinema!\n")
no_tickets = 0
total_cost = 0
while True:
if no_tickets == 0:
message = "Would you like to buy a ticket? (Yes/No) "
bye_message = "Ok, thanks for coming by."
elif no_tickets > 0:
message = "Would you like to buy another ticket? (Yes/No) "
bye_message = (f"Cool, you have purchased {no_tickets} tickets for a "
f"total cost of £{total_cost}.")
ticket = input(message)
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print(bye_message)
break
else:
print("Please answer 'Yes' or 'No'")
continue
Now this seems to work exactly the same as the previous program, but I'm confused about the if-elif chain. I thought that python executes only one block in an if-elif chain. So if the customer orders 1 ticket, then no_tickets > 0 and so we enter the second elif statement. Why then don't we go back to the start of the while loop and loop infinitely? Why instead do we continue to the other if statements below (testing if ticket.lower() == 'yes' or 'no')?
Thanks for reading all of this! Sorry if this seems like a pointless question as my code is working as intended, I just want to properly understand everything that's going on.
Upvotes: 0
Views: 160
Reputation: 11237
if your if statement
condition is fulfilled, say age<3 is true then statement for that condition are processed/executed and then your program will not see anything in if..elif..else
code block, it will go for the next code block(part of code after if..elif..else statement).
why we continue to other if statement, because in your code there are two code section for if statement and they processed one by one unless no break/exit statement didn't occur which cause them to go out of the while loop or exit the program
Upvotes: 0
Reputation: 74
This has to do with indentation. Languages like java enclose if/else statements in parenthesis {}, but python depends on indentation.
Notice that the testing if ticket.lower() == 'yes' or 'no'
has the same indentation as if no_tickets == 0:
and if no_tickets > 0:
therefore it is considered to be outside of the first if/else block.
This might be helpful: https://www.w3schools.in/python-tutorial/concept-of-indentation-in-python/
Upvotes: 1