Results 1 to 4 of 4

Thread: python check this one out

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    python check this one out

    Code:
    from sys import exit
        
    class adder(object):
        def __init__(self):
            self.a = []
            self.item = []
            self.argument=0        
        def dictCopy(*args):
            pass
        def adder(self,*args):
            for item in args:
                print item
                raw_input()
                if type(item)!=int and type(item) != float:
                        print 'not a number'
                        if type(item) == list:
                            print item
                            self.item = item
                            listOccourance(self.item)
                        continue
                else:
                    self.a.append(item)
                    print self.a
            print sum(self.a)
        def listOccourance(self, argument):
            self.argument = argument
            print self.argument
            print 'It seems that you have a list in here. How do you want to handle that?'
            print 'Press 1 to nest this list.\n Press 2 to take the sum thus far and append that to the list.'
            print 'Press 3 to do nothing.'
            choice = raw_input()
            if choice == 1:
                self.a.append(self.argument)
                print self.a
                raw_input()
            elif choice == 2:
                self.argument.append(sum(self.a))
                print self.argument
                raw_input
                exit(0)
            elif choice == 3:
                #this will default to back to list a
                pass
    no matter how I code this, I cannot get self.argument to append to self.a correctly.

  2. #2
    Join Date
    Jan 2012
    Beans
    161

    Re: python check this one out

    here is a sample of it running:>>> reload(adder)
    <module 'adder' from 'adder.py'>
    >>> d=adder.adder()
    >>> d
    <adder.adder object at 0x01B61F50>
    >>> d.adder(45,32,22,34,45,67,b,34,23)
    45
    [45]
    32
    [45, 32]
    22
    [45, 32, 22]
    34
    [45, 32, 22, 34]
    45
    [45, 32, 22, 34, 45]
    67
    [45, 32, 22, 34, 45, 67]
    [119, 210, 126, 9, 284, 154, 162, 262, 38, 20]
    not a number
    [119, 210, 126, 9, 284, 154, 162, 262, 38, 20]
    It seems that you have a list in here. How do you want to handle that?
    Press 1 to nest this list.
    Press 2 to take the sum thus far and append that to the list.
    Press 3 to do nothing.
    1
    34
    [45, 32, 22, 34, 45, 67, 34]
    23
    [45, 32, 22, 34, 45, 67, 34, 23]
    302
    >>>

  3. #3
    Join Date
    Feb 2009
    Beans
    1,469

    Re: python check this one out

    Take a closer look at this:
    Quote Originally Posted by wingnut2626 View Post
    Code:
            choice = raw_input()
            if choice == 1:

  4. #4
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python check this one out

    as explained here, raw_input returns a string, hence you have to convert that string into an int as follows
    Code:
    choice = int(raw_input())
    Last edited by alan9800; July 9th, 2013 at 08:09 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •