From 2caef31cef8ef79ec73de58f87869a25713f43db Mon Sep 17 00:00:00 2001 From: asddataking Date: Thu, 24 Mar 2022 18:18:10 -0400 Subject: [PATCH 1/2] Version 1 Lab 08 --- code/dan/python/lab08.py | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 code/dan/python/lab08.py diff --git a/code/dan/python/lab08.py b/code/dan/python/lab08.py new file mode 100644 index 00000000..8794c8cd --- /dev/null +++ b/code/dan/python/lab08.py @@ -0,0 +1,47 @@ +# Version 1 + +# get 16 digit credit card input +string_input = input("Please enter your 16 digit card number with no spaces: ") +print (string_input) + +#convert string to list +string_of_ints = [] # empty list to append integers +for item in string_input: # loop though number strings convert to numbers + item = int(item) + string_of_ints.append(item) + print (string_of_ints) + +# slice off the last digit +check_digit = string_of_ints[14:15:] +print (check_digit) + +#reverse the list +string_of_ints.reverse() +print (string_of_ints) + +# create list of every other digit +every_other_num = string_of_ints[::2] +print (every_other_num) + +# double every element in list +doubled_list = [num ** 2 for num in every_other_num] +print(doubled_list) + +# subtract 9 for numbers of 9 +minus_nine = [num-9 for num in doubled_list if num >= 9] +print (minus_nine) + +# sum all values +all_values = sum(minus_nine) +print (all_values) + +# get second digit +all_values = str(all_values) +validation_digit = all_values[1] +print (validation_digit) + +# validate card logic +if validation_digit == check_digit: + print ("This is a valid card") +else: + print ("This card is invalid") From 06965744f422674755c4d26be9c061c3d7c5ec69 Mon Sep 17 00:00:00 2001 From: asddataking Date: Thu, 24 Mar 2022 20:45:12 -0400 Subject: [PATCH 2/2] Updated Version 1 Lab 08 --- code/dan/python/lab08.py | 78 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/code/dan/python/lab08.py b/code/dan/python/lab08.py index 8794c8cd..11b02914 100644 --- a/code/dan/python/lab08.py +++ b/code/dan/python/lab08.py @@ -1,47 +1,37 @@ # Version 1 -# get 16 digit credit card input -string_input = input("Please enter your 16 digit card number with no spaces: ") -print (string_input) - -#convert string to list -string_of_ints = [] # empty list to append integers -for item in string_input: # loop though number strings convert to numbers - item = int(item) - string_of_ints.append(item) - print (string_of_ints) - -# slice off the last digit -check_digit = string_of_ints[14:15:] -print (check_digit) - -#reverse the list -string_of_ints.reverse() -print (string_of_ints) - -# create list of every other digit -every_other_num = string_of_ints[::2] -print (every_other_num) - -# double every element in list -doubled_list = [num ** 2 for num in every_other_num] -print(doubled_list) - -# subtract 9 for numbers of 9 -minus_nine = [num-9 for num in doubled_list if num >= 9] -print (minus_nine) - -# sum all values -all_values = sum(minus_nine) -print (all_values) - -# get second digit -all_values = str(all_values) -validation_digit = all_values[1] -print (validation_digit) - -# validate card logic -if validation_digit == check_digit: - print ("This is a valid card") +user_input = input("Please enter your 16 digit card number with no spaces: ") + +# create a function to for cc validation +def cc_check(card): + num_list = list(card) # Create a list + check_digit = num_list[14:15:]# Store check digit + print (check_digit) + num_list.reverse() # reverse the list + every_other = num_list[::2] # get every other element and add to new list + every_other = [int(i) for i in every_other] #turn list items to integers + doubled_list = [num * 2 for num in every_other] + print (doubled_list) + # for loop to find integers larger then 9 to subtract 9 + counter = 0 + for num in doubled_list: + if num >= 9: + num -= 9 + doubled_list[counter] = num + counter += 1 + print (doubled_list) + # sum all values on list + all_values = sum(doubled_list) + print (all_values) + all_values = str(all_values) + validation_digit = all_values[1] + if validation_digit == check_digit: + return True + else: + return False + +results = cc_check (user_input) +if results == True: + print("This credit card is valid") else: - print ("This card is invalid") + print ("This credit card is invalid") \ No newline at end of file