-1

Im attempting to write a code where the user if prompted for todays date and their birthdate in order to determine their age. The age will determine how much their ticket price will be. It would ask if the customer has a coupon, which would take off one dollar off their price. I have come up with this so far:

print ("Hello, welcome to Hopper's Computer Museum! To determine your enterance fee, please enter the following:")

print("Your date of birth (mm dd yyyy)")
Date_of_birth = input("--->")

print("Today's date: (mm dd yyyy)")
Todays_date = input("--->")


from datetime import date

def calculate_age(born):
    Todays_date = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

if age <= 14:
     price==5.00
elif age > 15 and age < 64:
    price==9.00
elif age > 65:
    price==7.50

print ('Do you have a coupon (y/n)?')
Discount = input("--->")
if Discount == "y" or Discount == "Y":
    price = price-1
elif Discount == "n" or Discount == "N":
    price = price

print ('Your admission fee is '+str(price))

I was looking at some similar questions and I they helped solve some of my problems but I am not sure what to define 'age' as so that the program will read it.

So after looking at the responses would it look more like this?

print ("Hello, welcome to Hopper's Computer Museum! To determine your enterance fee, please enter the following:")

print("Your date of birth (mm dd yyyy)")
Date_of_birth = input("--->")

print("Today's date: (mm dd yyyy)")
Todays_date = input("--->")


from datetime import date
def calculate_age(born):
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

age = calculate_age(Date_of_birth)

if age <= 14:
     price==5.00
elif age > 15 and age < 64:
    price==9.00
elif age > 65:
    price==7.50

print ('Do you have a coupon (y/n)?')
Discount = input("--->")
if Discount == "y" or Discount == "Y":
    price = price-1
elif Discount == "n" or Discount == "N":
    price = price

print ('Your admission fee is '+str(price))
2
  • 1
    What do you mean by "but I am not sure what to define 'age' as so that the program will read it."? Mar 16, 2017 at 0:43
  • 1
    When I run my program it asks the two questions but as soon as it receives an answer it says 'age' not defined
    – A. Gunter
    Mar 16, 2017 at 0:44

2 Answers 2

2

There are several problems in your code. Your high-level logic is good, but you're not implementing things fully. You get that error because you never give the variable age a value. Your calculate_age function will do the job if you bother to send it the right data and save the answer. Also note that you're making several assumptions about structure of a date, but you haven't written any code to give dates that structure.

You have two basic choices for that:

  1. Learn about Python's DateTime package and use the pre-defined routines there;
  2. Build your own data type.

Please note that your program has no date package and no structures today and born; the references to attributes of those objects will also come up undefined. You need to organize your program fully to get the required result.

To that end, I strongly recommend incremental programming: write one small block of code, and debug that until it works the way you expect. Then go on to the next block. Certainly, keep this program as a design reference, but don't depend on the names and flow you've used here.

For instance, start by assigning a particular birth date and a particular "today" date, in whatever form works for you. Iron out the logic. Make sure that you can compute the age (in whole years), and then assign the proper ticket price. After that, go back and work on date input.

1

You're never defining age, or calling calculate_age. Just call calculate_age with today's date:

def calculate_age(born):
    Todays_date = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

# Here. You need to define age
age = calculate_age(Date_of_birth)

if age <= 14:
     price==5.00
elif age > 15 and age < 64:
    price==9.00
elif age > 65:
    price==7.50

Note though, there's some funky stuff going on with your code. You ask the user for today's date, then overwrite what they enter with Todays_date = date.today(), then you never use it. Did you mean today = date.today()? Anyways, that's besides the question.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.