Python Forum
Credit card number redacting script
Thread Rating:
  • 4 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Credit card number redacting script
#1
Happy New Year Pythonistas!

I’m writing a Python shell script for fun. The purpose of the script is to ask the user for a fake credit card number (16 integers) and then returns a credit card number with the first 12 integer redacted with x’s.

I’m using a for loop with enumerate. I’m making a rookie mistake. I need some guidance.

Here is my script so far:

def ccredact(string):
   '''
   Take string input and then redact first 12 of 16 numbers
   '''
   spliced_list = list(string) # Convert string to list of numbers
  
   for index, value in enumerate(spliced_list):       
       # print(index, value) # test enumerate
       index[0:13] = value = 'x'
       result = spliced_list
       return result

string = str(input("Enter your fake credit card number: "))  # input something like 1234567812345678

ccredact(string)

if __name__ == "__main__":
   pass
In this script I define the function. In this function I first describe the functionality in a doc string. Then I splice the string of numbers into a list of numbers. Then I initiate the for loop using two variables (index and value) using enumerate. Then within the loop I try to re-assign the value of the first 12 integers to ‘x’ (this line is obviously my problem area, described below). After defining the function, I then name the string variable as I prompt the user to enter their fake 16 digit credit card number. Finally, I call the string.

Here is the output:
Error:
$ python script.py Enter your fake credit card number: 1234 Traceback (most recent call last): File "script.py", line 15, in <module> ccredact(string) File "script.py", line 9, in ccredact index[0:13] = value = 'x' TypeError: 'int' object does not support item assignment
The traceback is drawing my attention to line 9. I see two issues. The first is index and value are integers yet I am trying to assign an ‘x’ which is a string character. Though I’m not sure how to remedy this. The second issue at this line is that Python doesn’t support three variable assignments on a single line. I am not sure how to remedy this either. What would you people recommend? Any hints?
Reply


Messages In This Thread
Credit card number redacting script - by Drone4four - Jan-01-2019, 06:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 269 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,594 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  SQL wild card use hammer 3 1,379 Jan-07-2022, 02:17 PM
Last Post: hammer
  Validating credit card frequency 8 4,409 Nov-05-2018, 07:36 PM
Last Post: frequency
  individual's ID card number in python danpek 2 3,867 Jun-14-2018, 04:07 PM
Last Post: DeaD_EyE
  Prime number Script Problem Codezters 4 4,172 Jul-21-2017, 03:07 AM
Last Post: Larz60+
  RAW IO disk access for SD Card shift838 1 8,686 Feb-27-2017, 03:35 AM
Last Post: Larz60+
  "Card Dealing" Python code 2.7 Paradoxalis 3 6,783 Nov-17-2016, 11:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020