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

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0

    python error code


    Hello everyone
    I do not understand Python previously I was able to run the code in Python 3.6.2
    it does not work out getting error

    Traceback (last last call): file "/Users/davidcohen/Desktop/TEXS1233.py", line 26, for x in s: TypeRrror: iter () Return non-iterator type 'x' Enter code here


    class S:

    def __init__(self, data=[]):
    self.data = data[::]

    def push(self, v): self.data.append(v)

    def pop(self): return self.data.pop(-1)

    def __iter__(self):
    class x:
    def __init__(self, l): self.l = l[::]
    def next(self):
    if not self.l: raise StopIteration
    return self.l.pop(0)
    return x(self.data)
    s = S()
    s.push('2')
    s.push('X')
    s.push('1')


    for x in s:
    for y in s:
    x, y, '2', '2'

    for x in s:
    for y in s:
    x, y, 'x', '2'

    for x in s:
    for y in s:
    x, y, '1', '2'




    for x in s:
    for y in s:
    x, y, '2', 'x'

    for x in s:
    for y in s:
    x, y, 'x', 'x'

    for x in s:
    for y in s:
    x, y, '1', 'x'





    for x in s:
    for y in s:
    x, y, '2', '1'

    for x in s:
    for y in s:
    x, y, 'x', '1'

    for x in s:
    for y in s:
    x, y, '1', '1'
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,857
    Rep Power
    509
    Please simplify. What is class S supposed to be?
    What is the output you expect?
    Thanks, Dave
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0

    Smile


    Originally Posted by b49P23TIvg
    Please simplify. What is class S supposed to be? What is the output you expect? Thanks, Dave
     

    hello dave 

    I try to make all possible of 1,X,2

    Example output

    1,1
    1,x
    1,2

    x,1
    x,x
    x,2

    2,1
    2,x
    2,2


    Thanks a lot friend
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0
    class S:def __init__(self, data=[]): self.data = data[::]def push(self, v): self.data.append(v)def pop(self):return self.data.pop(-1)def __iter__(self):class x:def __init__(self, l): self.l = l[::]def next(self):ifnot self.l:raiseStopIterationreturn self.l.pop(0)return x(self.data) s = S() s.push('2') s.push('X') s.push('1')for x in s:for y in s: x, y,'2','2'for x in s:for y in s: x, y,'x','2'for x in s:for y in s: x, y,'1','2'for x in s:for y in s: x, y,'2','x'for x in s:for y in s: x, y,'x','x'for x in s:for y in s: x, y,'1','x'for x in s:for y in s: x, y,'2','1'for x in s:for y in s: x, y,'x','1'for x in s:for y in s: x, y,'1','1'
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0
    The code is clearer

    Code:
    class S:
        'S is for "stack".'
      
        def __init__(self, data=[]):
            self.data = data[::]  # = data would be a mistake.
      
        def push(self, v): self.data.append(v)
       
        def pop(self): return self.data.pop(-1)
       
        def __iter__(self):
            class x:
                def __init__(self, l): self.l = l[::]
                def next(self):
                    if not self.l: raise StopIteration
                    return self.l.pop(0)
            return x(self.data)
    
    
    s = S()
    s.push('2')
    s.push('X')
    s.push('1')
    
    for x in s:
        for y in s:
            x, y, '2', '2'
    
    for x in s:
    	for y in s:
    		x, y, 'x', '2'
    
    for x in s:
    	for y in s:
    		x, y, '1', '2'
    
    
    for x in s:
    	for y in s:
    		x, y, '2', 'x'
    
    for x in s:
    	for y in s:
    		x, y, 'x', 'x'
    
    for x in s:
    	for y in s:
    		x, y, '1', 'x'
    
    
    
    for x in s:
    	for y in s:
    		x, y, '2', '1'
    
    for x in s:
    	for y in s:
    		x, y, 'x', '1'
    
    for x in s:
    	for y in s:
    		x, y, '1', '1'
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,857
    Rep Power
    509
    Code:
    # Cartesian product is in standard library
    import itertools
    
    for pair in itertools.product('1x2', '1x2',):
        print(pair)
    The next method in your class X needs to be named __next__ .
    Code:
    class S:
        'S is for "stack".'
      
        def __init__(self, data=[]):
            self.data = data[::]  # = data would be a mistake.
      
        def push(self, v): self.data.append(v)
       
        def pop(self): return self.data.pop(-1)
       
        def __iter__(self):
            class x:
                def __init__(self, l): self.l = l[::]
                def __next__(self):
                    if not self.l: raise StopIteration
                    return self.l.pop(0)
            return x(self.data)
    
    
    s = S()
    s.push('2')
    s.push('X')
    s.push('1')
    
    n = 0
    
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '2', '2')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, 'x', '2')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '1', '2')
    
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '2', 'x')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, 'x', 'x')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '1', 'x')
    
    
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '2', '1')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, 'x', '1')
    print(n) ; n += 1
    for x in s:
        for y in s:
            print(x, y, '1', '1')
    Exercise, at which I failed while distracted:
    rewrite the __iter__ method so that it returns self instead of an object in a new type. Put the __next__ method into the class S.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0
    Thank you very much I'm going to work on it just for a long time I did not touch Python
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    9
    Rep Power
    0
    Code:
    
    import itertools
    for pair in itertools.product('1x2', '1x2', '1',):
        print(pair)
        
    print ('Thank you very much dave thats exactly what I needed')
  16. #9
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,857
    Rep Power
    509

    Cartesian product is standard verb in j.


    Code:
       NB. www.jsoftware.com
       {(2#<'1X2'),<'1'
    ┌───┬───┬───┐
    │111│1X1│121│
    ├───┼───┼───┤
    │X11│XX1│X21│
    ├───┼───┼───┤
    │211│2X1│221│
    └───┴───┴───┘
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo