-1

Trying to calculate a check-digit and add it to the end of generated credit card. So i want to concatenate two integers into a string. Problem is that this chunk of code generates a LIST of numbers rather than a number itself..

checkDigit = 0
while checksum % 10 != 0:
    checksum += 1
    checkDigit += 1
    cc_number = str(cc_number) + str(checkDigit)
return cc_number

So here i'm trying to calculate what number i need to add to checksum in order to produce a credit card which meets Luhn algorithm requirements. I'm expecting to get say number "5", but instead i get a list of 5 elements like "123456". And then it is concatenated to the end of first number... Why is that? I want to get for example: 2222222225 and NOT 222222222123456

  • This can't be all your code as you've got a `return` but no function definition, and you've not defined `checksum` before you've used it. Can you post a *working* example please. – Ari Cooper-Davis Oct 31 '19 at 19:43

2 Answers2

1

Unindent cc_number = str(cc_number) + str(checkDigit). It sounds like you only want to 'append' str(checkDigit) at the end of the loop, once you have determined the correct checkDigit. (I say 'append' because strings are immutable in python.)

Rekamanon
  • 217
  • 2
  • 10
0
checkDigit = 0
while checksum % 10 != 0:
    checksum += 1
    checkDigit += 1
return "{}{}".format(cc_number, checkDigit)

You don't want to concatenate the checkDigit inside the while loop. I put it in the return statement using string formatting.

Marie
  • 149
  • 9