List function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adam4u
    New Member
    • Mar 2010
    • 3

    List function

    How to do the function for input list of stock in python
    like [p(1), p(2), . . . , p(n)], for example [ 23,
    53, 55, 42, 31, 40 ]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You will have to clarify your question. I don't understand what you are trying to do.

    BV - Moderator

    Comment

    • Adam4u
      New Member
      • Mar 2010
      • 3

      #3
      i'm trying to do a functio that consists two input as arrays or (pairs)
      and here what i came up with
      Code:
      def main():
          i = [n]
          i, n = input("Enter any number: ")
          print " The first input is", i, " and the index is", n

      On other hand, I want to print out the number of any integer entered with its array index between bracket... Thanks in advance
      Last edited by bvdet; Mar 30 '10, 02:03 AM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Does this help any?
        Code:
        >>> numbers = [0,1,2,3,4,5,6,7,8,9]
        >>> for i, num in enumerate(numbers):
        ... 	print "The number is %s and the index is %s" % (num, i)
        ... 	
        The number is 0 and the index is 0
        The number is 1 and the index is 1
        The number is 2 and the index is 2
        The number is 3 and the index is 3
        The number is 4 and the index is 4
        The number is 5 and the index is 5
        The number is 6 and the index is 6
        The number is 7 and the index is 7
        The number is 8 and the index is 8
        The number is 9 and the index is 9
        >>>

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Hi Adam4u

          I'm afraid that code in your second post makes no sense. As I see it there are two things you could be trying to do.

          option 1: user enters a number, and the computer assigns it an index number automatically

          option 2: user enters a number and an index and the computer assigns that number to that index.

          Both are possible, of course. Which do you want? Or is there something else that you want?

          Here's an ugly implementation of option 1:
          Code:
          p=[]  #sets up p as a list
          while True:  #infinite loop for data input
              i=input("Enter any number (0 to quit): ")
              if i==0: break   #if user enters nothing break out of infinite loop
              p.append(i)  #append i as the next item in the list
          
          #Once we've broken out of the loop, print of the values you've entered:
          print p
          #or if you want to be a bit fancy
          for n,i in enumerate(p):
              print "index ", n, "value ", i, " which is equal to ", p[n]

          Comment

          • Adam4u
            New Member
            • Mar 2010
            • 3

            #6
            bvdet and Glenton thanks for your help... i just figured it out from online search, Glenton you were closed to what i wanted.. but thanks

            Comment

            Working...