Jan-01-2019, 06:10 PM
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:
Here is the output:
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__": passIn 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?