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. If they are 14 and younger the price would be five dollars. If they are 15 to 64 the price would be nine dollars and if they are 65 and older the price would be 7.50. It would ask if the customer has a coupon, which would take off one dollar off their price. What I have 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 ('Todays Date: (mm dd yyyy)')
Todays_Date = input("--->")


age = (tYear-bYear)
if (bMonth > tMonth):
    age == age-1
if (bMonth == tMonth and
    bDay > tDay):
    age == age-1

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 $4.00 enjoy your visit!')

I know that I need define my variables such as tYear, bYear, bDay, ect, but I'm not sure what I should assign them to.

A. Gunter
  • 112
  • 1
  • 2
  • 14
  • You should use the technique from Danny W. Adair's answer in Kumar's link. To convert a date string in your 'mm dd yyyy' format to a `date` object you can do `from datetime import datetime` ... `dob = datetime.strptime(dob_string, '%m %d %Y').date()` – PM 2Ring Feb 21 '17 at 08:59
  • 1
    Your age `if` test's not quite right. That should be `if age <= 14:`. Also, you can chain comparisons in Python, so you can do stuff like `elif 15 < age < 64:`. And that last `elif` is redundant, just make it an `else:`. Similarly, there's no need to test for both yes and no responses in the discount test. – PM 2Ring Feb 21 '17 at 09:13

1 Answers1

1

You can use map to assign values to tYear, bYear etc. You can do following instead of Date_of_birth = input('--->')

    bDay,bMonth,bYear = map(int,raw_input('--->').split())

This will assign bDay, bMonth and bYear as per requirement if the input format is 'dd mm yyyy'. Similarly for today's date you can write:

    tDay,tMonth,tYear = map(int,raw_input('--->').split())

Alternate option is to calculate age using datetime. Follow this - Age from birthdate in python

Community
  • 1
  • 1
Kumar Gaurav
  • 156
  • 1
  • 1
  • 8