Luhn Python中的校验和

时间:2018-06-27 12:09:33

标签: python python-3.x

大家好,我是python初学者,我已经在python版本3中实现了Luhn校验和,我希望您对我的代码有意见和看法,我知道这是令人尴尬的代码,并且不遵循python的最佳做法,但可以!

这是我的代码。

import ast

def  doubledigit(n,x) :

    if x is  True:

        doubledigit = n *2
        sum=0
        if doubledigit >= 10 :
            sum = 1 + doubledigit % 10
        else: sum = doubledigit
    else:
       sum = n
return (sum)
def sum ():
   total =0
   prop = int(input("enter how many digits you want: " ))
   for i in range (0,prop):
       n = int(input("Enter your digit: " ))
       x = input("Enter True or False: ")
       total = total+doubledigit(n,ast.literal_eval(x))

   return total
def findx (n):
   if (n % 10 == 1 ):
      n += 9
   if (n % 10 == 2 ):
      n += 8
   if (n % 10 == 3 ):
      n += 7
   if (n % 10 == 4 ):
      n += 6
   if (n % 10 == 5 ):
      n += 5
   if (n % 10 == 6 ):
      n += 4
   if (n % 10 == 7 ):
      n += 3
   if (n % 10 == 8 ):
      n += 2
   if (n % 10 == 9 ):
      n += 1
   if (n % 10 == 0):
      print("vaild checksum")
return n
print(findx(sum()))

0 个答案:

没有答案