2

Hey guys I'm fairly new to the programming world. For a school practice question I was given the following text and I'm suppose to convert this into code. I've spent hours on it and still can't seem to figure it out but I'm determine to learn this. I'm currently getting the error

  line 7, in <module> if i % 2 == 0: TypeError: not all arguments converted during string formatting 

What does this mean? I'm still learning loops and I'm not sure if it's in the correct format or not. Thanks for your time.

# GET user's credit card number
# SET total to 0
# LOOP backwards from the last digit to the first one at a time
    # IF the position of the current digit is even THEN
        # DOUBLE the value of the current digit
        # IF the doubled value is more than 9 THEN
            # SUM the digits of the doubled value
        # ENDIF
       # SUM the calculated value and the total
    # ELSE
        # SUM the current digit and the total
    # ENDIF
# END loop
# IF total % 10 == 0 THEN
    # SHOW Number is valid
# ELSE
    # SHOW number is invalid
# ENDIF


creditCard = input("What is your creditcard?")
total = 0
for i in creditCard[-1]:
    if i % 2 == 0:
        i = i * 2
        if i > 9:
            i += i
    total = total + i

    else:
        total = total + i

if total % 10 == 0:
    print("Valid")

else:
    print("Invalid")
2
  • 1
    creditCard[-1] gives you only the last element. creditCard[::-1] gives you the reverse of creditCard instead (or just use reversed(creditCard)). Commented Oct 17, 2016 at 1:24
  • are you using Python 2 or 3? if 3 you need to convert the input to an integer Commented Oct 17, 2016 at 1:54

1 Answer 1

1

well, i can see 2 problems:

1)when you do:

for i in creditCard[-1]

you dont iterate on the creditCard you simply take the last digit. you probably meant to do

for i in creditCard[::-1]

this will iterate the digits from the last one to the first one

2) the pseudocode said to double the number if its POSITION is even, not if the digit itself is even

so you can do this:

digit_count = len(creditCard)
for i in range(digit_count -1, -1, -1):
    digit = creditCard[i]

or have a look at the enumerate built-in function

edit:

complete sample:

creditCard = input("What is your creditcard?")
total = 0
digit_count = len(creditCard)
for i in range(0, digit_count, -1):
    digit = creditCard[i]

    if i % 2 == 0:
        digit = digit  * 2
        if digit  > 9:
            digit = digit / 10 + digit % 10 # also noticed you didnt sum the digits of the number  

    total = total + digit



if total % 10 == 0:
    print("Valid")

else:
    print("Invalid")
Sign up to request clarification or add additional context in comments.

5 Comments

The first part makes complete sense thank you. For the second, how would I integrate that ? II'm a little confused on what this means.
i added a complete version, with another fix, you didnt SUM the digits of the doubled value as your assigment said, what youd did was doubling the number again (youdid i += i so if i = 2 this will make it 4) . you can sum the digits by doing digit / 10 + digit % 10 because deviding by 10 will give the second digit ( it is an integer so it cant have a decimal point so 14/ 10 == 1) and the digit % 10 will give you the first digit
THANK YOU THANK YOU! I appreciate that so much. One final question and I'll stop bugging you but what does this exactly mean for i in range(0, digit_count, -1):
Nevermind I searched the documentation. I appreciate this more than you know. You've taught me so much. Thank you again :)
range is a built in fanction that returns an array with number from start(the first parameter) to stop (the second parameter), at the given steps ( the third parameter) so if you call range(0, 5) for example will return [0,1,2,3,4] and range(6, 0, -1) will return [6,5,4,3,2,1], here are its docs:docs.python.org/2/library/functions.html#range . btw i did an error in the code, i just fixed it. it needs to be range(digit_count-1 ,-1, -1) cause it will start from the first parameter to the second parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.