Jump to content
Sign in to follow this  
ramin92003

Python To Autoit

Recommended Posts

Hello,

How can I convert my code from Python to Autoit? Because I'm not familiar with Autoit. For example, how can I convert this part of my code:

import sys

degree = input("Enter the degree [BA / BSC / BIS / BBA] :")

name = ""

maxEnrollment = 80

if degree == "BA" or degree == "BSC" or degree == "BIS" or degree == "BBA":

    if degree == "BA":

        name = "Department of Bachelor of Arts"

elif degree == "BSC":

    name = "Department of Bachelor of Science"

elif degree == "BIS":

    name = "Department of Information Systems"

elif degree == "BBA":

    name = "Department of Bachelor of Administration"
else:

    print("\nINVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")

    sys.exit()

main.py

Share this post


Link to post
Share on other sites

:welcome: to the forum.

Try this:

;~ import sys

_Example()
Exit

Func _Example()
    Local $degree = InputBox("Enter the degree [BA / BSC / BIS / BBA] :", '')
    Local $name = ""
    Local $maxEnrollment = 80

    If $degree == "BA" Or $degree == "BSC" Or $degree == "BIS" Or $degree == "BBA" Then

        If $degree == "BA" Then
            $name = "Department of Bachelor of Arts"
        ElseIf $degree == "BSC" Then
            $name = "Department of Bachelor of Science"
        ElseIf $degree == "BIS" Then
            $name = "Department of Information Systems"
        ElseIf $degree == "BBA" Then
            $name = "Department of Bachelor of Administration"
        Else
            ConsoleWrite("\nINVALID $degree PROGRAM - PLEASE ENTER A NEW $degree PROGRAM" & @CRLF)
        EndIf
    EndIf
EndFunc   ;==>_Example

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest beginning - communication with GitHub REST API Forum Rules *
Include Dependency Tree (Tool for analyzing script relations)
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

PDF Related:https://www.autoitscript.com/forum/topic/177368-how-to-get-reference-to-pdf-object-embeded-in-ie/ *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2020-09-20

Share this post


Link to post
Share on other sites

I polished up @mLipok's script to look more like what a normal AutoIt script would look like instead of being an exact translation :)

Local $degree = InputBox("Enter the degree [BA / BSC / BIS / BBA] :", '')
Local $name = ""
Local $maxEnrollment = 80

If $degree = "BA" Or $degree = "BSC" Or $degree = "BIS" Or $degree = "BBA" Then
    If $degree = "BA" Then
        $name = "Department of Bachelor of Arts"
    ElseIf $degree = "BSC" Then
        $name = "Department of Bachelor of Science"
    ElseIf $degree = "BIS" Then
        $name = "Department of Information Systems"
    ElseIf $degree = "BBA" Then
        $name = "Department of Bachelor of Administration"
    Else
        ConsoleWrite(@CRLF & "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")
    EndIf
EndIf

Few things to note:

  1. The equal operator can be used for both comparison and assignment in AutoIt, the meaning changes based on the context
  2. No string substitution is done in AutoIt, so you have to manually construct a string by the concatenation/joining operator (&)
  3. @CRLF is a special variable (called a macro) which is equal to /r/n in python string substitution. (@LF is equal to just \n)
  4. Most AutoIt development is done on a GUI basis rather than console-based input/output, ConsoleWrite is used for quick runtime debugging or to output fairly technical information, most of the time you would be using the MsgBox and InputBox functions to interact with the users.
  5. Don't under-estimate the awesome help file which is an all round reference to the AutoIt language, it comes installed with AutoIt and you can press F1 in SciTE to open it :)

By the way :welcome:welcome to AutoIt Forums.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites
3 hours ago, ramin92003 said:

Department of Bachelor of Science

Thank you so much for your help. I have rewritten my code with AutoIt script but the problem is, I can't connect my first part of my code to the second part. For example, when I enter a correct degree acronym and click on the OK button the code doesn't go to the next part to get the number of students from year 1 to 6. How can I connect these two part with the OK button?

 

#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $hDLL = DllOpen("user32.dll") 

$degree = InputBox("Enter the degree ","Enter the degree acronym: ","")
$maxEnrollment = 80
Dim $year1, $year2, $year3, $year4, $year5, $year6



If $degree = "BA" Then
MsgBox (0, "Info","Department of Bachelor of Arts")
ElseIf $degree <> "BA" Then
    MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM.")
    Exit (1)
EndIf

If $degree = "BSC" Then
MsgBox (0, "Info","Department of Bachelor of Science")
ElseIf $degree <> "BSC" Then
    MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")
    Exit (1)
EndIf
If $degree = "BIS" Then
MsgBox (0, "Info","Department of Information Systems")
ElseIf $degree <> "BIS" Then
    MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")
    Exit (1)
EndIf
If $degree = "BBA" Then
MsgBox (0, "Info","Department of Bachelor of Administration")
ElseIf $degree <> "BBA" Then
    MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")
    Exit (1)
EndIf


$year1 = InputBox("year 1 Students ","The # of students in Year 1: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf

$year2 = InputBox("year 2 Students ","The # of students in Year 2: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year3 = InputBox("year 3 Students ","The # of students in Year 3: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year4 = InputBox("year 4 Students ","The # of students in Year 4: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year5 = InputBox("year 5 Students ","The # of students in Year 5: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year6 = InputBox("year 6 Students ","The # of students in Year 6: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf

$total = $year1 + $year2 + $year3 + $year4 + $year5 + $year6
; Display Results
$sMessage = "This is a report"
$title = "Student Enrollment"
SplashTextOn($title, $sMessage, -1, -1, -1, -1, 4, "")
ControlSetText($title, "", "Static1", "The Total Number of Students: " & $total& @CRLF)



While 1
    If _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        Sleep(500)
        ExitLoop
    EndIf
WEnd

 

Share this post


Link to post
Share on other sites

@Ramin You are not properly using the If...ElseIf...EndIf statements, combine them like shown in the previous post by @mLipok and me. Currently no matter what, all of the If conditions are executed so you are guaranteed to hit a wrong "degree" error and then the program would exit.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Thank you for your quick response. I have fixed the If...ElseIf...EndIf statements based on @mLipok  and @TheDcoder posts, but when I enter a wrong degree acronym, it doesn't show the INVALID DEGREE PROGRAM message. Do I need to use while loop for this section?

#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $hDLL = DllOpen("user32.dll") ; set local variable to capture user pressing ESC key

$degree = InputBox("Enter the degree ","Enter the degree acronym: ","")
Local $name = ""
$maxEnrollment = 80
Dim $year1, $year2, $year3, $year4, $year5, $year6



If $degree = "BA" or $degree = "BSC" or $degree = "BIS" or $degree = "BBA" Then
    If $degree = "BA" Then
        $name = MsgBox (0,"Info", "Department of Bachelor of Arts")
    ElseIf $degree = "BSC" Then
        $name = MsgBox(0, "Info","Department of Bachelor of Science")
    ElseIf $degree = "BIS" Then
        $name = MsgBox(0,"Info","Department of Information Systems")
    ElseIf $degree = "BBA" Then
        $name = MsgBox(0, "Info", "Department of Bachelor of Administration")
    Else

MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")


    EndIf
EndIf


$year1 = InputBox("year 1 Students ","The # of students in Year 1: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf

$year2 = InputBox("year 2 Students ","The # of students in Year 2: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year3 = InputBox("year 3 Students ","The # of students in Year 3: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year4 = InputBox("year 4 Students ","The # of students in Year 4: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year5 = InputBox("year 5 Students ","The # of students in Year 5: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf
$year6 = InputBox("year 6 Students ","The # of students in Year 6: ","")
If $year1 > $maxEnrollment Then
MsgBox(5, "Warning", "Exceeds the limit")
EndIf

$total = $year1 + $year2 + $year3 + $year4 + $year5 + $year6
; Display Results
$sMessage = "This is a report"
$title = "Student Enrollment"
SplashTextOn($title, $sMessage, -1, -1, -1, -1, 4, "")
ControlSetText($title, "", "Static1", "The Total Number of Students: " & $total& @CRLF)


; Wait until ESC key is released.
While 1
    If _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        Sleep(500)
        ExitLoop
    EndIf
WEnd

 

Share this post


Link to post
Share on other sites
If $degree = "BA" or $degree = "BSC" or $degree = "BIS" or $degree = "BBA" Then
    If $degree = "BA" Then
        $name = MsgBox (0,"Info", "Department of Bachelor of Arts")
    ElseIf $degree = "BSC" Then
        $name = MsgBox(0, "Info","Department of Bachelor of Science")
    ElseIf $degree = "BIS" Then
        $name = MsgBox(0,"Info","Department of Information Systems")
    ElseIf $degree = "BBA" Then
        $name = MsgBox(0, "Info", "Department of Bachelor of Administration")
    Else
        ; this is inside "outer IF THEN ENDIF"
        MsgBox(5, "Info", "INVALID DEGREE PROGRAM - PLEASE ENTER A NEW DEGREE PROGRAM")
    EndIf
EndIf

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest beginning - communication with GitHub REST API Forum Rules *
Include Dependency Tree (Tool for analyzing script relations)
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

PDF Related:https://www.autoitscript.com/forum/topic/177368-how-to-get-reference-to-pdf-object-embeded-in-ie/ *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2020-09-20

Share this post


Link to post
Share on other sites

@ramin92003 Great! It can be intimidating at first but you will get good at writing AutoIt after some practice :thumbsup:


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Consider separating data from logic. Example:

#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>

Global Enum  $ARRCOL_ABBR, _
             $ARRCOL_TITLE, _
             $ARRCOL__ENUM

Global Const $g_sDelmCol  = @TAB
Global Const $g_sFileCSV  = 'data.csv'
Global Const $g_sMsgInp   = 'Enter acronym :'
Global Const $g_sMsgInfo  = '%s for #%i "%s".'
Global Const $g_sMsgFail  = 'No match for "%s".'
Global Const $g_sInput    = InputBox(@ScriptName, $g_sMsgInp)

Global       $g_aDataCSV

_FileReadToArray($g_sFileCSV, $g_aDataCSV, $FRTA_COUNT, $g_sDelmCol)

For $i1 = 1 To $g_aDataCSV[0][0]

    If $g_sInput = $g_aDataCSV[$i1][$ARRCOL_ABBR] Then

        MsgBox($MB_ICONINFORMATION, @ScriptName, StringFormat($g_sMsgInfo, $g_aDataCSV[$i1][$ARRCOL_ABBR], $i1, $g_aDataCSV[$i1][$ARRCOL_TITLE]))
        ExitLoop

    ElseIf $i1 = $g_aDataCSV[0][0] Then

        MsgBox($MB_ICONERROR, @ScriptName, StringFormat($g_sMsgFail, $g_sInput))

    EndIf
Next

Content of data.csv (TAB delimited) :

BA  Department of Bachelor of Arts
BSC Department of Bachelor of Science
BIS Department of Information Systems
BBA Department of Bachelor of Administration

Array could also be declared in script (instead of separate file); prevents source code maintenance if data changes.

Edited by user4157124

Share this post


Link to post
Share on other sites
Global Enum  $DATA_YEAR, _
             $DATA_ENROLMAX, _
             $DATA_ENROLCUR, _
             $DATA__ENUM

Global Const $g_sMsg  = 'Enter amount for year %i please:'
Global Const $g_sLine = 'year %i,' & @TAB & 'limit %i,' & @TAB & 'amount %i,' & @TAB & 'pct %.2f\n'
Global Const $g_sEnd  = '===\namount total %i\n'

Global       $g_iSum  = 0
Global       $g_sRes  = ''
Global       $g_aData = [ _; year, limit, amount
                            [1, 80, 0], _
                            [2, 80, 0], _
                            [3, 80, 0], _
                            [4, 80, 0], _
                            [5, 80, 0], _
                            [6, 80, 0] _
                        ]

For $i1 = 0 To UBound($g_aData) - 1

    $g_aData[$i1][$DATA_ENROLCUR] = Int(InputBox(@ScriptName, StringFormat($g_sMsg, $g_aData[$i1][$DATA_YEAR])))
    $g_iSum                      += $g_aData[$i1][$DATA_ENROLCUR]
    $g_sRes                      &= StringFormat($g_sLine, $g_aData[$i1][$DATA_YEAR], $g_aData[$i1][$DATA_ENROLMAX], $g_aData[$i1][$DATA_ENROLCUR], ($g_aData[$i1][$DATA_ENROLCUR] / $g_aData[$i1][$DATA_ENROLMAX] * 100))
;   If $g_aData[$i1][$DATA_ENROLCUR] > $g_aData[$i1][$DATA_ENROLMAX] Then PromptOrJustReadTheReport()

Next

$g_sRes &= StringFormat($g_sEnd, $g_iSum)
ConsoleWrite($g_sRes)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...