Converting code from VBA to python language. Struggling to check my work

Viewed 1416

I need to convert this VBA code into python. The code in vba form is as the following:

Function Findit(x As Integer, y As Integer, z As Integer) As Integer
Findit = x + y + z
If (y = x) Then
Findit = y
If (y < z) Then
Findit = z
EndIf
If (Findit <> x) Then
Findit = y
EndIf
Else
If (z = Findit) Then
Findit = y
Findit = x
EndIf
EndIf
End Function

So what I did is walked through line by line and tried to do the indentation correctly because I know it matters in Python. My temptation is as the following:

def Findit_f(x, y, z):
    Findit = x + y + z
    
    if x == y:
        Findit += y
        if y < z:
            Findit += z
       
    elif Findit != x:
        Findit += y
    
    else:
        if z == Findit:
            Findit += x
            Findit += y
        
        return Findit

But there is not a way to check if it is true or not. There is not really an error but that is the case and I need someone to verify my work with an explanation (if there is an error) so I could understand what is going on! I'm new to python and that's why I'm struggling. Also, is there any way to check if I'm doing it correctly or not? like printing a value or something. Because I tried that but once I run the code there is no error and also there is no results. It just seem to be running successfully but I'm not sure if I met the criteria of the VBA code in python. Thank you again and sorry for any confusion! if there is something that needed to be clarified just let me know! Thank you!

1 Answers

Here is a website for converting vba to python The vb2py online converter is still a work in progress but it may help you as a reference point.

An example of your code could be similar to the following:

'
' Multi-line and singe-line ifs are converted to blocks in Python

Function Findtit(x, y, z)
    Dim subLocal
    subLocal = x + y + z
    
    If (y = x) Then
    Findit = y
    
    ElseIf (y < z) Then
    Findit = z
    
    ElseIf (Findit <> x) Then
    Findit = y
    
    Else
    If (z = Findit) Then
    Findit = y
    Findit = x
    End If
    End If
End Function

In pyhton this would look like:

from vb2py.vbfunctions import *
from vb2py.vbdebug import *

"""
Multi-line and singe-line ifs are converted to blocks in Python
"""
def Findtit(x, y, z):
    subLocal = Variant()
    subLocal = x + y + z
    if ( y == x ) :
        Findit = y
    elif  ( y < z ) :
        Findit = z
    elif  ( Findit != x ) :
        Findit = y
    else:
        if ( z == Findit ) :
            Findit = y
            Findit = x
    return fn_return_value
Related