入学价格根据年龄而定

时间:2017-03-10 05:55:16

标签: python

我试图提出一个程序,您可以根据年龄计算入场价格。价格为:14及以下($ 5.00),15至64($ 9.00),以及65及以上($ 7.50)。客户可能还有一张优惠券,可以从他们的价格中扣除1美元。到目前为止,我想出了:

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)')

我很困惑的一件事是如何让Python获取用户输入的日期并将其放入我设置的年龄计算中。

2 个答案:

答案 0 :(得分:1)

您可以指定日期格式,然后将其拆分为......

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)

或者您可以参考以下链接获取更多信息......

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

答案 1 :(得分:0)

如果您希望使用固定格式,用户将提供dd/mm/yyyymm/dd/yyyy

等日期

然后您可以使用:

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