Home > OS >  adding together double digit numbers as two individual digits
adding together double digit numbers as two individual digits

Time:10-28

I get a user to input their 'credit card number' (11 numbers) and assign it to variable card_number. I then use the card_number.split() function to turn the input into a list and assign it to variable card_numbers.

I then want to iterate through the list of numbers, starting from the right most number, moving left, and doubling every second digit. If the product of doubling every second digit is >9 (eg 7 * 2 = 14) I would then sum the digits of the product (1 4 = 5). If the product of doubling a digit is < 10, I leave as is. This is my code thus far:

card_number = input('Please enter your 11 digit card number: ')
card_numbers = card_number.split()

list1 = []

total = 0 

for num in card_numbers[-2:0:-2]:
    num = int(num) * 2
    if num >= 10:
        list1.append(num)
        
    if num < 10:
        total = total   num
    

print(total)
print(list1)

My question is, if the product of doubling a number is > 10, eg 7 * 2 = 14, how do I add the 1 and the 4 together?

CodePudding user response:

Just pull out the digits individually.

    if num > 9:
        total  = num//10   num % 10

CodePudding user response:

You don't have to declare variable card_numbers since there's no space in your input variable. This should work

card_number = input('Please enter your 11 digit card number: ')
#card_numbers = card_number.split()
list1 = []

total = 0 

for num in card_number[-2:0:-2]:
    num = int(num) * 2
    if num >= 10:
        list1.append(num)
        total  = num//10   num%10
        
    if num < 10:
        list1.append(num)
        total = total   num
    

print(total)
print(list1)

CodePudding user response:

You cannot use split(),as it is not separated by either space or any other character, either convert card number to list or directly slice it both will work equally:

Also in your code :

if num >= 10: list1.append(num) if num < 10: total = total num

Your requirement is quite opposite of the code you have written:

If the product of doubling every second digit is >9 (eg 7 * 2 = 14) I would then sum the digits of the product (1 4 = 5). If the product of doubling a digit is < 10, I leave as is.

I have rewritten the code which you can see which value it is choosing in each loop and you can do as you want, Hopefully it will help in finding solution of your requirement:

card_number = input('Please enter your 11 digit card number: ')
card_numbers = list(card_number)
print(card_numbers)
list1 = []

temp = 0 

for num in card_numbers[-2:0:-2]:
    double_num = int(num) * 2
    
    if double_num >= 10:
        # finding each each digit in the 2 digit number and summing up each digit
        total = double_num//10 double_num%10
        
    if double_num < 10:

        temp=double_num
    # checking each value 
    print(num,"",double_num," ",total," ",temp)
    # Reseting the value to 0
    total=0
    temp=

The Above output is :

Please enter your 11 digit card number: 123456789
['1', '2', '3', '4', '5', '6', '7', '8', '9']
8  16   7   0
6  12   3   0
4  8   3   8
2  4   3   4
  • Related