#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    16
    Rep Power
    0

    Question user input and arrays


    well ive been hunting around the forum and i still cant figure it out. im new with python and having trouble.

    i need to know what line of code i use to let a user enter numbers into an array in python.

    so far i have nothing, any advice would help me a ton!

    thanks

    -glittery
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hi,
    There could be an efficient way I suppose! This too would work fine.


    Code:
    >>> def UserArray():
    ... 	times=input("Enter the no. of elements in the array")
    ... 	arr=[]
    ... 	for number in range(times):
    ... 		myNum=input("Enter number")
    ... 		arr.append(myNum)
    ... 	return arr
    ...
    Subha
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    If u want the user to enter only positive numbers, then you could make use of the array module ....

    PHP Code:
    >>> def UserInput():
    ...     
    import array
    ...     
    arr=array.ArrayType('I'#unsigned integers only...2 bytes of memory
    ...     times=input("Enter the no. of array elements")
    ...     for 
    number in range(times):
    ...         
    myNum=input("enter the number")
    ...         
    arr.append(myNum)
    ...     return 
    arr
    ... 
    A word about arrays...

    Arrays represent basic values and behave very much like lists, except the type of objects stored in them is constrained.
    Subha
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    16
    Rep Power
    0
    thanks, but i enter both the programs in and i keep getting an error

    >>> no module named py
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Suppose you are saving your program as glitter.py

    So is this the error you get.....

    PHP Code:
    >>> import glitter.py
    Traceback 
    (most recent call last):
      
    File "<interactive input>"line 1in ?
    ImportErrorNo module named py 
    DON'T use glitter.py

    the correct method is.....

    PHP Code:
    >>> import glitter
    >>> glitter.UserInput()
    array(
    'I', [10L20L]) 
    Subha

IMN logo majestic logo threadwatch logo seochat tools logo