0

I try to translate my code from c to python. I review the code several times and I think that is equal, but the program don't works despite in c works prefectly. This is my implementation:

"""Know what credit card you have or if is invalid
* When the program is running, type the number of you credit card"""

from cs50 import get_int
from math import pow

#counter, to store every number of the credit card that 
#the program will take
c = 0

c_number = get_int("Enter the number of you credit card \n")

#Storing the second last number of the credit card multiplied by 2
warehouse = ((c_number / 10)%10)*2

#if the stored number have two digits will be greater than 10 so:
if warehouse >= 10:
#Taking and adding the first and second number and adding them to a counter
c += warehouse / 10 + warehouse % 10
else: #the stored number have one digit
    #Adding to the counter
    c += warehouse

#Taking new credit card values and storing them
warehouse = c_number / 1000

#Creating another warehouse
warehouse2 = 0

#Loop to constantly change the value of warehouse
while warehouse > 0:
    warehouse2 = (warehouse % 10)*2

#Doing the same thing that before the difference is that 
#at the  end of the condition the value of warehouse is modified
if warehouse2 >= 10:
    c += warehouse2 / 10 + warehouse2 % 10
else:
    c += warehouse2

warehouse /= 100

warehouse = c_number

#Doing the same thing that before, the difference is  that it's not
# multiplied by 2 so you don't need a conditional
while warehouse > 0:
    c += warehouse % 10
    warehouse /= 100

#card verification
if c % 10 == 0:
    #Saying what type of card the user have
    if (c_number >= 34*pow(10,13) and c_number < 35*pow(10,13)) or (c_number >= 37*pow(10,13) and c_number < 38*pow(10,13)):
        print("AMEX")

    elif c_number >= 51*pow(10,14) and c_number < 56*pow(10,14):
        print("MASTERCARD")

    elif (c_number >= 4*pow(10,12) and c_number < 5*pow(10,13)) or (c_number >= 4*pow(10,15) and c_number < 5*pow(10,15)):
         print("VISA")
    else:
        print("INVALID")

else:
    print("INVALID")

If I type for example 378282246310005 (number of a AMEX credit card) give me as output INVALID

2

If you were to print(warehouse) after this line

warehouse = ((c_number / 10)%10)*2

you'd discover that it returns a floating point number. Python doesn't do integer division like C unless you use the // operator (integer division operator).

1
  • You're right, thanks!!!! Jun 16 '18 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .