【问题标题】:Admission price based on age入场价格基于年龄
【发布时间】:2017-07-31 09:02:42
【问题描述】:

我正在尝试开发一个程序,您可以在其中根据年龄计算入场费。价格为:14 岁及以下(5.00 美元)、15 至 64 岁(9.00 美元)和 65 岁及以上(7.50 美元)。客户也可能有一张优惠券,可以从他们的价格中扣除一美元。到目前为止,我想出了:

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 获取用户输入的日期并将其放入我设置的年龄计算中。

【问题讨论】:

  • 您可能希望使用= 来分配值而不是==(检查是否相等)...现在我考虑了一下,请仔细检查您所有的ifelif声明。
  • @Jenna 我希望你能得到你的答案,如果有,请在下方select the correct answer

标签: python


【解决方案1】:

如果您想要一个固定的格式,用户将提供日期,例如 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 

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

    • 我会把这个放在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2017-01-25
    相关资源
    最近更新 更多