0

Would someone please point me in the right direction regarding my sum1 variable?

I'm trying to store the values of the doubled nums from the variable doubled. To me it looks like im performing the same operations I used to get the double variable, but that's clearly not working out when i try to print for debugging. This is my first foray into python. For card ive been entering: 1234567890987 I understand the error, but I don't know how to fix it. I'd appreciate any feedback or pointers Thank you for your time and effort

from cs50 import get_int, get_string


 def main():
 # get an integer from the user to ensure only int imputs
   card = get_int("Card: ")
 # convert the card into a string so it may be indexed to manipulate individual digits
   card = str(card)

 # check len to make sure its the correct amount of numbers that would be in a major credit card.
    if len(card) < 13 or len(card) > 16:
        print("invalid len")
        exit(1)


# iterate over every other number in the card starting with the second to last digit

    for i in range(len(card)-2, 0, -2):
        doubled = (int(card[i]) *2)
        
        #add the string values together
        sum1 = sum(int(doubled))
        print(sum1)
main()

The terminal reads:

Traceback (most recent call last):
  File "/home/ubuntu/pset6/credit/credit.py", line 22, in <module>
    main()
  File "/home/ubuntu/pset6/credit/credit.py", line 20, in main
    sum1 = sum(int(doubled))
TypeError: 'int' object is not iterable
Powerbyte
  • 5
  • 2

1 Answers1

0

The sum() function expects an iterator whereas you are giving it a integer. Pherhaps you are trying to use sum1 += int(doubled).

Yogesh Bhandari
  • 460
  • 2
  • 9
  • Thank you! Such a simple thing cost me some hours lol! I should have asked for help earlier. You made my day – Powerbyte Jun 18 '21 at 01:20