Question

In: Computer Science

I am creating a program in Python that allows the user to input a credit card...

I am creating a program in Python that allows the user to input a credit card number, and determine if the card is valid. This should be done by taking every other number, starting from the right, and adding them together. The doubling each of the other digits, and adding them together as single digits, and then adding the two sums together. For example, if the number 4833 1200 3412 3456 is used, the first sum should be 6+4+2+4+0+2+3+8=29, the second sum would be 1+0+6+2+6+0+2+6+8=31. Adding the two sums together should be 60. A number that is divisible by 10, ex 60, would be a valid number. A number that is not divisible by 10, ex 35, is not a valid number.

After the validity of the card is determined, the program must then determine what the "check number" should be if the card is not valid. This would be the very last number of the card sequence. For example, if the number 5424 1810 5676 1902 is given, the program should see the number is invalid and determine the last number should be a 6 instead of a 2 for the card to be valid.

My current code tells if the cards is valid or not but should also accept numbers with any number of spaces in between, I'm not sure how to fix that either. Here is my code so far:

creditCardNum=input("Enter card number")

def sum_digits(n):
r=0
while n:
r, n=r + n %10 , n//10
return r

sum1=0
sum2=0

for i in range(1,len(creditCardNum),2):
sum1=sum1+int(creditCardNum[i])

for i in range(0,len(creditCardNum),2):
data=2*int(creditCardNum[i])
sum2=sum2+sum_digits(data)

total=sum1+sum2
total=str(total)

if total[-1]=='0':
print("The card is valid")
else:
print("The card is not valid")

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

creditCardNum=input("Enter card number : ")
#remove unnecessary spaces
creditCardNum = creditCardNum.replace(" ","")
def sum_digits(n):
r=0
while n:
r, n=r + n %10 , n//10
return r

sum1=0
sum2=0

for i in range(1,len(creditCardNum),2):
sum1=sum1+int(creditCardNum[i])

for i in range(0,len(creditCardNum),2):
data=2*int(creditCardNum[i])
sum2=sum2+sum_digits(data)

total=sum1+sum2
remain = total%10
if remain==0:
print("The card is valid")
else:
cardLastNum = int(creditCardNum[-1])
print("The card is not valid")
remainVal = cardLastNum+10-remain
remainVal = remainVal%10
print("The last digit should be %d instead of %d"%(remainVal,cardLastNum))


Related Solutions

I am creating a crop watering "simulator" in Python. I have the user input an array...
I am creating a crop watering "simulator" in Python. I have the user input an array and then must compare the placement of water and crops to determine if all the crops in the array are watered. The user will either input a "w" for water or "c" for crop when creating the array. A w cell can water 8 cells around it, including itself. My end result must determine if all the crops will be watered or not. How...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
JAVA - I am asking for the user to input their firstName and lastName but I...
JAVA - I am asking for the user to input their firstName and lastName but I want the method myMethod() to be able to print it when it is called. Is that possible? I'm new to Java and i'm not too sure what I should do to fix this. Also if I were to create a IF statement would I have to declare int amountDeposited; , int accountBalance; , int newBalance; in the new if statement. import java.util.Scanner; import java.util.Arrays;...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
9) This is in Python Code a program that allows the user to manage its expenses...
9) This is in Python Code a program that allows the user to manage its expenses accepting the following commands: - Add <player_name> -> This command adds a new player to the system. Assume there can’t be repeated names. If a name already exists then an error is shown to the user. - Score <player_name> <score> -> This command adds the score to the player with the name player-name. Each score needs to be added individually - Report -> This...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT