# Credit.py Runtime Errors

My code has multiple errors, according to the Python interpreter. This is strange as I have simply ported, line to line, from C to Python. Can someone please help me to understand why my code is producing such errors? Thank you in advance.

Code:

``````#Returns company and validity of credit card number
from cs50 import get_int

def main():
#Inputs positive credit card
credit = 0
while credit <= 0:
credit = get_int("Credit card number: ")
#Checks company of credit card
companyname = company(credit)
#Checks validity of credit card
if (validity(credit) == 1):
#Prints company
print(companyname);
else:
#Prints invalidity
print("INVALID")

def validity(x):
#Creates an array of ints to get every second digit doubled
multiplied = []
length = digits(x)
#Gets second-last digit doubled
copy = x / 10
multiplied.append(copy % 10 * 2)
for i in range(length / 2):
#Gets every second digit doubled
copy /= 100
multiplied.append(copy % 10 * 2)
#Adds the array's digits together
halfsum = 0
for m in range(length / 2):
l = multiplied[m]
for j in range(digits(multiplied[m])):
k = l % 10
l /= 10
halfsum += k
summed = halfsum
#Adds digits that were not multiplied together
notmult = x % 10
copy = x
for n in range (length):
copy /= 100
notmult += copy % 10
summed += notmult
#Checks if last digit is 0
if summed % 10 == 0:
return 1
else:
return 0
return 0

#Returns company of a credit card number
def company(x):
#Finds length of CC number
length = digits(x)
#Checks if CC is AMEX
if length == 15:
copy = x
while copy >= 100:
copy /= 10
if copy == 34 or copy == 37:
final = "AMEX"
else:
final = "INVALID"
#Checks if CC is VISA
elif length == 13:
copy = x
while copy >= 10:
copy /= 10
if copy == 4:
final = "VISA"
else:
final = "INVALID"
#Checks if CC is MasterCard or VISA
elif length == 16:
copy = x
cop = x
while copy >= 100:
copy /= 10
while (cop >= 10):
cop /= 10
if copy == 51 or copy == 52 or copy == 53 or copy == 54 or copy == 55:
final = "MASTERCARD";
elif cop == 4:
final = "VISA"
else:
final = "INVALID"
#Determines invalid if none other is true
else:
final = "INVALID"
return final

#Returns how many digits there are in a number
def digits(x):
digits = 0
copy = x
#Keeps dividing and counting until number is 0
while copy != 0:
copy /= 10
digits += 1
#Returns 1 if number originally was 0
if x == 0:
digits += 1
return digits

if __name__ == "__main__":
main()
``````

Error:

``````    Traceback (most recent call last):
File "credit.py", line 111, in <module>
main()
File "credit.py", line 12, in main
if (validity(credit) == 1):
File "credit.py", line 26, in validity
for i in range(length / 2):
TypeError: 'float' object cannot be interpreted as an integer
``````

Python 3 separated integer division `//` and floating point division `/` operators. You probably want to use the former (note that it truncates the result, so you might have to use `(length + 1) // 2` for rounding up). Oh, and there's `//=` operator, too.

Something about the nested loops makes me feel uncomfortable. I don't understand them, or why one would want nested loops on that one. Maybe you can explain them, though.

BTW,

``````if copy == 51 or copy == 52 or copy == 53 or copy == 54 or copy == 55:
``````

can be expressed as

``````if copy in [51, 52, 53, 54, 55]:
``````

or

``````if 51 <= copy <= 55:
``````

(note that the last one is special to Python and won't work in C, though it does not give a compile error, just unexpected results)