Jump to content

Recommended Posts

Posted

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

Posted

: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:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted
  On 10/29/2018 at 2:23 AM, ramin92003 said:

Department of Bachelor of Science

Expand  

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

 

Posted

@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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

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

 

Posted
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:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

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
Posted
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)

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...