Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me figure out what I\'m doing wrong? The program is supposed to

ID: 3718528 • Letter: C

Question

Can someone help me figure out what I'm doing wrong?

The program is supposed to:

1. Accepts as input a text file named “card_data.txt” which contains several fictitious sixteen-digit credit card numbers as individual records,
validates each record as a sixteen-digit number for length,
2. uses Luhn’s Algorithm to determine if the number in each record is a valid sixteen-digit credit card number

Code

# Open file card_data.txt

file = open('card_data.txt', 'r')

# Read each line in file and strip

card_num = file.readline().strip()

card_num = int(card_num)

# Close the file

file.close()

# Check card length

def card_len():

index = 0

while card_num != '':

if card_num != 16:

print('This is not a valid card.')

else:

print('This card is a valid.')

index += 1

return card_num

# Check card_num follows alogrithim

def card_check():

length = len(card_num)

odd_sum = 0

even_sum = 0

if (length ==0):

return 0

else:

if(length % 2 == 0):

last = card_nun[-1]

even_sum += last

return even_sum + length

  

def main():

card_len()

card_check()

main()

Explanation / Answer

Everything is fine with the code, there are just type conversion mistakes at some places in your code. I have corrected them and commented the explanation. # Open file card_data.txt file = open('card_data.txt', 'r') # Read each line in file and strip card_num = file.readline().strip() card_num = int(card_num) # Close the file file.close() # Check card length def card_len(): index = 0 # card_num is int and you cannot compare it with string # so convert card_num to string by : str(card_num) while str(card_num) != '': # again card_num is int. And you cannot find the length of int directly # first convert the int to string and then find the length # as i have demonstrate : len(str(card_num)) if len(str(card_num)) != 16: print('This is not a valid card.') else: print('This card is a valid.') index += 1 return card_num # Check card_num follows alogrithim def card_check(): # again card_num is int. You cannot find the length of int directly # first convert int to string and then find its length length = len(str(card_num)) odd_sum = 0 even_sum = 0 if (length == 0): return 0 else: if (length % 2 == 0): # here you are trying to find the last digit of the card_num # but card_num is int. Andyou cannot find the last digit of int directly # first convert the int to string and then use [-1] operation to find last digit last = str(card_num)[-1] # now the 'last' stores the string value of last digit # So for adding last digit to even_sum convert last to int by : int(last) even_sum += int(last) return even_sum + length def main(): card_len() card_check() main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote