Reputation:
I'm trying to come up with a program where you can calculate the admission price based on age. The prices are: 14 and under ($5.00), 15 to 64 ($9.00), and 65 and over ($7.50). The customer may also have a coupon that will take a dollar off of their price. So far I have come up with:
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
price = -1
while price == -1:
try:
age = int(input('age:'))
excpet ValueError:
print("Not a number, try again.")
continue
if age <= 14:
price==5.00
elif age > 15 and age < 64:
price==9.00
else 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)')
One thing that I am confused on would be how to get Python to take the dates that the user inputs and put it into the age calculation that I set up.
Upvotes: 1
Views: 687
Reputation: 4330
If you want to have a fixed format in which user will provide the date like dd/mm/yyyy
or mm/dd/yyyy
Then you can simply use:
import datetime
date_time_object = datetime.datetime.strptime(date_provided, date_format)
#date_provided = '12/01/1995'
#date_format = '%d/%m/%Y'
curr_time = datetime.datetime.now()
time_diff = curr_time - date_time_object
print time_diff
age_days = time_diff.days
age_years = age_days / 326.25
print age_years
Upvotes: 0
Reputation: 1093
you can specify the format in which you want to take date and then split it like ...
import datetime
date_entry = input('Enter a date in YYYY-MM-DD format')
year, month, day = map(int, date_entry.split('-'))
date1 = datetime.date(year, month, day)
Or you can refer the following links for more info...
getting-input-date-from-the-user-in-python-using-datetime-datetime
how-to-have-user-input-date-and-subtract-from-it
how-do-i-take-input-in-the-date-time-format
Upvotes: 1