我試圖編寫一個代碼,如果用戶提示今天的日期和他們的出生日期,以確定他們的年齡。年齡將決定他們的門票價格是多少。它會詢問客戶是否有優惠券,這會從他們的價格中扣除一美元。我想出了這個至今:計算入場費價格
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))
我一直在尋找一些類似的問題,我他們幫助解決了一些自己的問題,但我不知道要定義什麼「年齡」爲使該程序會讀取它。
因此,看到迴應後,它會看起來更像這樣嗎?
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))
你是什麼意思?「但我不知道該怎麼定義」年齡「,以便程序能夠讀取它。」? – Carcigenicate
當我運行我的程序時,它提出了兩個問題,但只要它收到一個答案,它說'年齡'沒有定義 –