1

I'm new at Python and I try to figure out of to enter a list of numbers into array. This is what I did: Ask the user for a number

iNum = int(input("Please enter your number: "))

Find the length

iLen=len(str(iNum))

Enter the digits into array

a=[]
for i in range(0,iLen,1):
    a[i].append=iNum%10
    iNum=iNum//10

it doesn't work and I can't understand why.. I even try to do a[i]=iNum%10.

so could you please assist?

2
  • Are you trying to store each digit of your string into list? Commented Feb 3, 2013 at 8:51
  • I need to take a number from the user, than print the count each digit is appear in the number. What want to do is to take the number and enter each digit into a[i] like in C++. Commented Feb 3, 2013 at 8:59

5 Answers 5

2

There are a couple of confusing points to your code. What exactly is your end goal? You want all digits of a single number in the array? Or you want the user to enter multiple numbers?

Even with those things that confuse me there are still some things I can see are erroneous:

a[i].append=iNum%10

This is destined to fail right from the get-go: since a has been declared empty (a = []), there is no a[i] element. You can try this code out in an interactive environment like IDLE:

>>> a = []
>>> a[0] = 'hello'
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a[0] = 'hello'
IndexError: list assignment index out of range

You probably meant to call the append method on lists. However, to call that method, you don't use the equals sign, you use parenthesis. Something like so:

a.append(iNum % 10)

Another thing to note is your call to range is slightly superfluous. Since iterating from 0 to some number by a step of 1 is so common, it's the default.

range(iLen)

Putting it all together, we wind up with this:

a=[]
for i in range(iLen):
    a.append(iNum%10)
    iNum=iNum//10

If you want to get the digits of a single number into a list, you can simply use the list function on your string, like so:

>>> list('123')
['1', '2', '3']

And in Python, you can loop over the characters of a strings using the for loop. So if you want to convert each character to an integer, you can even do something like this:

a = []
for digit in str(iNum):
    a.append(int(digit))
Sign up to request clarification or add additional context in comments.

Comments

1

.append() is a method that appends items to the end of your list. The way you wrote it isn't correct:

a.append(iNum % 10)

A simpler way of doing what you're trying to do would be with a list comprehension:

number = input("Please enter your number: ")  # You want to keep it as a string
a = [int(digit) for digit in number]

Or even shorter with map():

a = map(int, number)

2 Comments

Bear in mind that input() only returns a string in Python 3. In Python 2 you should use raw_input()
And in python3 map doesn't return a list.
1

I need to take a number from the user, than print the count each digit is appear in the number.

#! /usr/bin/python3.2

n = input ("Please enter your number: ")

for digit in map (str, range (10) ):
    print ('{} appears {} times in your number.'. \
        format (digit, len ( [c for c in n if c == digit] ) ) )

Comments

0

This: a[i].append=iNum%10 is wrong because you bind the reference to a callable method to a int object. Apart from that, when you append to a list, it automatically inserts an object to the end of the list. If you would like to place something in a specific position inside a list, consider using the insert() method.

To instantly fix your code, call it like this: a.append(iNum%10)

2 Comments

When I do that, I get: IndexError: list index out of range
@YanivOfer Yeah, you're right, it was because I blindedly copied and pasted your code. Try running it now.
0

Method 1:

sizeofarray=int(input())
arr=[]

for _ in range(sizeofarray):
    inputvalue = int(input("Enter value: "))
    arr.append(inputvalue)

or

Method 2:

arr=[int(i) for i in input().split()]

#Note For Method 2 the values in spaces example: 4 8 16 32 64 128 256
#The result array will look like this arr = [4,8,16,32,64,128,256]

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.