Reputation: 1171
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?
Upvotes: 1
Views: 12730
Reputation: 1539
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]
Upvotes: 0
Reputation: 32429
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] ) ) )
Upvotes: 1
Reputation: 9805
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)
Upvotes: 0
Reputation: 4387
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))
Upvotes: 2
Reputation: 298106
.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)
Upvotes: 1