7 kyu The Coupon Code
Task
Task
Your mission:
Write a function called checkCoupon which verifies that a coupon code is valid and not expired.
A coupon is no more valid on the day AFTER the expiration date. All dates will be passed as strings in this format: "MONTH DATE, YEAR"
Verbalization
Step1 クーポンコードの確認
1. entered_codeと correct_codeが同じだったら、Trueを返す
2.entered_codeとcorrect_codeが違っていたらFalseを返す
Step2 日付の確認
1.current_dateがexpiration_dateより前または同じだったら、Trueを返す
2.current_dateがexpiration_dateより後だったら、Falseをかえす。
Code
from datetime import datetime
def check_coupon(entered_code, correct_code, current_date, expiration_date):
if entered_code != correct_code:
return False
current = datetime.strptime(current_date, "%B %d, %Y")
expire = datetime.strptime(expiration_date, "%B %d, %Y")
return current <= expire
Reference
datetime:日付オブジェクトに変換
current = datetime.strptime(current_date, "%B %d, %Y")
expire = datetime.strptime(expiration_date, "%B %d, %Y")
strptime とは?
strptime は "string parse time"(文字列を日付へ変換)
日付のフォーマットを指定しないと変換できない
%B → 月名(January, February, ...)
%d → 日
%Y → 年(4桁)
例
"June 15, 2020" → datetime(2020, 6, 15)