i am very new to python language and i am asked to write the following code
can any one please help
write flowchart python code to implement the algorithm of mobile prepaid charging cards considering the following:
- the system should generate charging numbers consisting of 14 numbers.
- the generated number should contains the charging amount for example (10, 50, 100).
- the system should give error messages if the number is fabricated, the number is already charged before or the charging data is expired.
please i need your help asap
thanks in advance
What code do you have so far? What do you need help with?
chargingcardPrefixList = [
['5', '0'], ['5', '0'], ['5', '0'], ['1', '0'], ['1', '0', '0']]
def completed_number(prefix, length):
"""
'prefix' is the start of the CC number as a string, any number of digits.
'length' is the length of the CC number to generate. Typically 13 or 16
"""
ccnumber = prefix
# generate digits
while len(ccnumber) < (length - 1):
digit = str(generator.choice(range(0, 10)))
ccnumber.append(digit)
# Calculate sum
sum = 0
pos = 0
reversedCCnumber = []
reversedCCnumber.extend(ccnumber)
reversedCCnumber.reverse()
while pos < length - 1:
odd = int(reversedCCnumber[pos]) * 2
if odd > 9:
odd -= 9
sum += odd
if pos != (length - 2):
sum += int(reversedCCnumber[pos + 1])
pos += 2
# Calculate check digit
checkdigit = ((sum / 10 + 1) * 10 - sum) % 10
ccnumber.append(str(checkdigit))
return ''.join(ccnumber)
def charging_card_number(rnd, prefixList, length, howMany):
result = []
while len(result) < howMany:
ccnumber = copy.copy(rnd.choice(prefixList))
result.append(completed_number(ccnumber, length))
return result
def output(title, numbers):
result = []
result.append(title)
result.append('-' * len(title))
result.append('\n'.join(numbers))
result.append('')
return '\n'.join(result)
#
# Main
#
generator = Random()
generator.seed() # Seed from current time
charging_card = credit_card_number(generator, chargingcardPrefixList, 14, 10)
print(output("charging_card_numb", charging_card))
here:
generator = Random()
Random isn’t defined anywhere.
i only need the code that checks the validity of card numbers
but that is an existing algorithm named Luhn Algorithm, no need to re-invent the wheel
ok great can you help me in allying this algorithm to my code above
but the code you posted above doesn’t even work.
with a simple google search we can just find the algorithm:
https://stackoverflow.com/questions/21079439/implementation-of-luhn-formula