pythonB
pythonB

Reputation: 65

Booking Tickets Program in Python

I'm fairly new to Python and have been working on some exercises over the weekend. I'm stuck on a particular one at the moment which requires me to make a cinema booking tickets program.

I'm stuck on working out how to give percentage values to certain ages, I've left some comments in my code as for what I need to include some how.

There needs to be a 10% discount and 15% discount given to the standard ticket price of 14.95.

I'm required to use a function called 'buyTicket()' which I have included with a parameter for the percentage working out step.

As of current the program is able to identify the ages but I'm unsure on how to add percentages to the certain ages and then total the ticket prices up after each ticket has been selected.

Below is my current code. Any advice on what to include and use is greatly appreciated!

Thank you.

def cinema():
age = int(input("Please enter your age: "))

if age < 18:
    print("Sorry. You are not over 18 years old. You cannot watch this movie.")

if age >= 18:
     print("You are allowed to see the film. Standard ticket price is £14.95 ")

#twentydiscount = 10% (over 20)
#oapdiscount = 15% (over 65)

def buyTicket(percentageDiscount):
    if age <= 20:
        print("You are under 20 years old. You are eligible for a 10% discount. Your ticket price will be", percentageDiscount)

    elif age >= 65:
        print("You are 65 years or over. You are eligible for a 15% discount. Your ticket price will be", percentageDiscount)
    else:
        print("You are", age, "years old. Your ticket price will be £14.95")

# totalticketprice = (adding up prices of tickets after tickets have been selected each time)

while (True):
   cinema()
   anotherticket = input("Do you want to find out more information about another ticket? (Yes/No): ")
   if anotherticket == 'No':
    exit()
buyTicket(percentageDiscount)

Upvotes: 2

Views: 10894

Answers (3)

jbndlr
jbndlr

Reputation: 5210

Try to not mix up logic and interface (prints for the user) too much. Always keep an eye on your logic first:

  • Store and keep the original price in a variable
  • Store the discount in a variable, depending on age of buyer
  • Collect all information you need
  • Calculate in the end
  • Inform the user where necessary

This is an example for your script (with shortened sentences..):

#!/usr/bin/python3


def cinema():
    age = int(input('Enter age: '))

    if age < 18:
        print('  Too young for this movie.')
        return

    discount = get_discount(age)
    print_discount_message(discount)

    price = 14.95
    discd = calculate_discount_price(price, discount)

    print(f'  Your price: {discd} (original price: {price})')


def get_discount(age):
    if age <= 20:
        discount = 0.1
    elif age >= 65:
        discount = 0.15
    else:
        discount = 0.0

    return discount


def print_discount_message(discount):
    if discount == 0.0:
        print('  Not qualified for discount.')
    else:
        print('  Qualified for discount: {}%'.format(int(discount * 100)))


def calculate_discount_price(original_price, discount):
    return round(original_price - original_price * discount, 2)


if __name__ == '__main__':
    while True:
        cinema()
        more = input('Buy more? (Yes/No): ')
        if more != 'Yes':
            break

Exemplary output:

$ python3 cinema.py 
Enter age: 17
  Too young for this movie.
Buy more? (Yes/No): Yes
Enter age: 19
  Qualified for <= 20 discount (10%).
  Your price: 13.45 (original price: 14.95)
Buy more? (Yes/No): Yes
Enter age: 21
  No discount.
  Your price: 14.95 (original price: 14.95)
Buy more? (Yes/No): Yes
Enter age: 66
  Qualified for >= 65 discount (15%).
  Your price: 12.71 (original price: 14.95)
Buy more? (Yes/No): Nah

Upvotes: 1

tygzy
tygzy

Reputation: 714

If I have read this right I think this is what you want:

n = £14.95
x = n / 100
percent = n * x - 100
price = abs(age * percent / 100)

I think this is right for what you are trying to do the first 3 lines is the calculation to get a percentage from a number.

Upvotes: 0

SerAlejo
SerAlejo

Reputation: 493

First define a function to calculate the percentage of a number. You could do that inline, but you will need it multiple times, so using a function for this is nicer coding:

def percentage(price, pct):
    return (pct * price) /100

You can then call this function where it is needed like this:

print("You are 65 years or over. You are eligible for a 15% discount. Your ticket price will be", percentage(price, percentageDiscount))

You will also have to create the price variable.

P.S.: Your question smells like homework ;)

Upvotes: 0

Related Questions