0

I'm having issues with this list, I want to times each number by a certain number, but at the moment each data in the list is a string, and if I turn the list into an integer or turn the string into an integer and make it into an list, I get an error.

def main():
    isbn = input("Enter you're 10 digit ISBN number: ")
    if len(isbn) == 10 and isbn.isdigit():
        list_isbn = list(isbn)
        print (list_isbn)
        print (list_isbn[0] * 2)
    else:
        print("Error, 10 digit number was not inputted and/or letters were inputted.")
        main()


if __name__ == "__main__":
    main()
    input("Press enter to exit: ")
4
  • 3
    You don't have an array, you have a list; there is a big difference in Python. Commented Jan 27, 2014 at 20:24
  • What errors do you get? What output were you expecting instead? Commented Jan 27, 2014 at 20:26
  • I don't know if this is what you need or not but changing lines 3 and for by following code make the code compile. if len(str(isbn)) == 10 and str(isbn).isdigit(): list_isbn = str(isbn) Commented Jan 27, 2014 at 20:35
  • 1
    @sk4x0r: this is Python 3, I'd say, where input() returns a string. Commented Jan 27, 2014 at 20:40

3 Answers 3

5

You'd turn each individual character into an integer with:

list_isbn = [int(c) for c in isbn]

Demo:

>>> isbn = '9872037632'
>>> [int(c) for c in isbn]
[9, 8, 7, 2, 0, 3, 7, 6, 3, 2]
Sign up to request clarification or add additional context in comments.

Comments

0

@Martijn Pieters answer will not work because input is reading the items as integers already, and in his example he defined the ISBN as a string. --

The problem is that the len builtin function is for sequences (tubles, lists, strings) or mappings (dictionaries) and isbn is an int.

The same holds true for isdigit.

I've posted a working program below:

def main():
    # number to multple by
    digit = 2
    isbn = input("Enter you're 10 digit ISBN number: ")
    # Liste generator that turns each the string from input into a list of ints
    isbn = [int(c) for c in str(isbn)]

    # this if statement checks to make sure the list has 10 items, and they are
    # all int's
    if len(isbn) == 10 and all(isinstance(item, int) for item in isbn):
        # another list generator that multiples the isbn list by digit
        multiplied = [item * digit for item in isbn]
        print multiplied
    else:
        print("Error, 10 digit number was not inputted and/or letters were inputted.")
        main()


if __name__ == "__main__":
    main()
    input("Press enter to exit: ")

Comments

0

Change this line

isbn = input("Enter you're 10 digit ISBN number: ")

to

isbn = raw_input("Enter you're 10 digit ISBN number: ")

Comments

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.