python中设置的cs50信用问题的解决方案

时间:2019-05-16 15:53:24

标签: python cs50

在用数字credit.py测试我的378282246310005时,它使我无效

from cs50 import get_int

def main():
    while True:
        h = int(input("Enter credit number: "))
        if h >0:
            break

    odd_sum_digit=0
    even_sum_digit=0

    while (h > 0):
        last_digit= h % 10
        odd_sum_digit += last_digit
        even_digit= int((h / 10) % 10)

        if (2 * even_digit) > 9:
              even_digit = (2 * even_digit)-9
        else:
            even_digit = 2 * even_digit

        even_sum_digit += even_digit

        h=h//100

    sum= odd_sum_digit + even_sum_digit
    print(f"{sum}")  #Test that checksum works.


   if (sum % 10 == 0):

        if ((h >= 4000000000000 and h < 5000000000000) or (h >= 4000000000000000 and h < 5000000000000000)):
            print("VISA")

        elif ((h >= 340000000000000 and h < 350000000000000) or (h >= 370000000000000 and h < 380000000000000)):
            print("AMEX")

        elif (h >= 5100000000000000 and h < 5600000000000000):
            print("MASTERCARD")

        else:
            print("INVALID")
  else:
        print("INVALID")


if __name__=="__main__":
    main()

1 个答案:

答案 0 :(得分:0)

如果检查整数的允许范围,则会发现您的数字超出范围。 考虑使用字符串代替