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

    Join Date
    May 2016
    Posts
    3
    Rep Power
    0

    Running step by step code Python Like Visual Basic


    Hi I am Germán from Mexico,

    First I ask for an apology my English is not that good.

    I have a very special request I would really appreciate if someone can help me out, this can be only answered with someone with experience in Python and Visual Basic.
    First able, I attach in this topic an image: the thing is that the “For” is quite similar in both language, for example in Python there is a for x in [1,2,3] and in VBA is for each x in Data, I write a quick example in the image attach, everything is perfect there.
    However the problems comes when I try to do in Python the other kind of “For” that is used in Visual Basic.

    1-¿ Anyone have any kind of idea how to do this “For” in Python?

    2- This is the most important question: As you see in the image there is a part that is marked in yellow in the program Visual Basic, that happens when you click “F8” it let you run the program step by step, also it let you see what’s the value of a variable after running the variable, also it let you use “Play” and when the programs fails it stops where it was and market in yellow, sometimes I did it In purpose to know why a certain loop fail in the row “x” and think about it when you have 100,000 rows.… ¿it this is possible in Python or Notepad c++?, because this is the way I do debugging in Visual Basic I can’t think another way to debug a program that reads more than 1,000,000 lines of data.
    2.1 ¿It is possible run step by step if yes, how? Some people say to use this in Phython:

    import pdb
    pdb.set_trace()
    it wasn’t really helpful

    2.2.- Can you see the value of the variable in a certain way as VBA?

    3- ¿What’s is your opinion of all this? ¿How can you debug in Python or what kind of technic do we have to use?

    My version of pyhon is 2.7.6
    [IMG][/IMG]
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,857
    Rep Power
    509
    For integers,

    If I recall BASIC: for i = 1 to n step dn

    would be in python

    for x in range(1,n+dn,dn):
    [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
    May 2016
    Posts
    3
    Rep Power
    0
    Hmm, i don’t really get what does the “step” , i don’t really understand neither the python way... :/ no that pro in programming, did you mind explaining to me?

    and for the other question did you know something?

    Thank you by the way!!
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,857
    Rep Power
    509
    step of BASIC for loop https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
    Code:
    For...Next Statement (Visual Basic)
    
    Visual Studio 2015 Other Versions 
     
    Repeats a group of statements a specified number of times.
    Syntax
    For counter [ As datatype ] = start To end [ Step step ]
        [ statements ]
        [ Continue For ]
        [ statements ]
        [ Exit For ]
        [ statements ]
    Next [ counter ]

    $ python3
    >>> for i in range(4,12,4): print(i)
    ...
    4
    8
    >>> for i in range(4,12+4,4): print(i)
    ...
    4
    8
    12
    >>>


    Here's a debugger session. I recommend using set_trace() from within a script.
    command s to single step
    command p expression to print the result of an expression
    Code:
    >>> # define a function
    >>> def sales_tax(price, rate=0.08):
    ...     return price * rate
    ... 
    >>> # use the function
    >>> print(sales_tax(__name__))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in sales_tax
    TypeError: can't multiply sequence by non-int of type 'float'
    >>> 
    >>> 
    >>> 
    >>> # find out why
    >>> import pdb
    >>> def debug_me():
    ...     pdb.set_trace()
    ...     print(sales_tax(__name__))
    ... 
    
    >>> debug_me()
    > <stdin>(3)debug_me()
    (Pdb) s
    --Call--
    > <stdin>(1)sales_tax()
    (Pdb) s
    > <stdin>(2)sales_tax()
    (Pdb) p price
    '__main__'
    (Pdb) p rate
    0.08
    (Pdb) type(price)
    <class 'str'>
    (Pdb) # oh!  there's the problem.  I tried by multiply a string by a float.
    *** SyntaxError: unexpected EOF while parsing
    (Pdb) q
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in debug_me
      File "<stdin>", line 2, in sales_tax
      File "<stdin>", line 2, in sales_tax
      File "/usr/lib/python3.5/bdb.py", line 48, in trace_dispatch
        return self.dispatch_line(frame)
      File "/usr/lib/python3.5/bdb.py", line 67, in dispatch_line
        if self.quitting: raise BdbQuit
    bdb.BdbQuit
    >>> 
    >>> 
    >>> 
    >>> 
    >>> # fix the problem
    >>> from numbers import Number
    >>> def sales_tax(price, rate=0.08):
    ...     if not all(isinstance(value, numbers.Number) for value in (price, rate,)):
    ...         raise TaxError('expected numbers from which to compute sales tax')
    ...     return price * rate
    ... 
    >>> 
    >>> 
    >>> class TaxError(TypeError):    # need to define TaxError
    ...     pass
    ... 
    >>> 
    >>> 
    >>> sales_tax(7, '0.07')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in sales_tax
    __main__.TaxError: expected numbers from which to compute sails tax
    >>> 
    >>> 
    >>> sales_tax(7, 0.07)
    0.49000000000000005
    >>> sales_tax(7,)  # default
    0.56
    >>> # it works now.

    Comments on this post

    • ghostpanther agrees
    Last edited by b49P23TIvg; May 29th, 2016 at 12:24 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo