"""Program used to check if a credit card is authentic."""
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Check it out
import datetime
class Customer:
"""Class representing the customer and their credit card details"""
# Constructor
def __init__(self):
self.name = input("Name: ")
self.postcode = input("Postcode: ")
self.card_date = input("Card date: ")
self.card_code = input("Card code: ").strip()
def check_date(self):
"""Checks current date against the credit card's date. If it is valid, returns True; else False."""
card = datetime.datetime.strptime(self.card_date, "%d/%m/%Y").date()
if datetime.date.today() < card:
return True
else:
return False
def check_code(self):
"""Contains the algorithm to check if the card code is authentic"""
code_list = list(str(self.card_code))
check_digit = int(code_list[7])
code_list.pop()
# The last digit is assigned to be a check digit and is removed from the list.
code_list.reverse()
for item in code_list:
temp_location = code_list.index(item)
if isEvenis_even(temp_location):
code_list[temp_location] = int(item) * 2
# Loops over each digit, if it is even, multiplies the digit by 2.
for item in code_list:
temp_location = code_list.index(item)
if int(item) > 9:
code_list[temp_location] = int(item) - 9
# For each digit, if it is greater than 9; 9 is subtracted from it.
sum_list = 0
for item in code_list:
sum_list += int(item)
# Calculates the sum of the digits
code_total = sum_list + int(check_digit)
if code_total % 10 == 0:
return True
else:
return False
# If the code is divisible by 10, returns True, else, it returns False.
def check_auth(self):
"""Checks the card's authenticity. """
if self.check_code() and self.check_date():
print("----------------------")
print("Valid")
print(self.name)
print(self.postcode)
print(self.card_date)
print(self.card_code)
else:
print("----------------------")
print("Invalid")
print(self.name)
print(self.postcode)
def is_even(number):
"""Function used to test if a number is even."""
if number % 2 == 0:
return True
else:
return False
if __name__ == "__main__":
customer().check_auth()