Android Question Rewrite python for B4X

Python:
myList=[24, 53, 76]

Num=int(input("Enter a number to check if it is a lucky number"))

if Num in myList:
print("You entered a lucky number")
else:
print("Sorry, try again later")

Please, how do i re-write this code in B4X?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X is mostly UI based. There is no console in mobile apps.
B4X:
Private Sub Button1_Click
 Dim MyList As List = Array(24, 53, 76)
 If MyList.IndexOf(EditText1.Text) > -1 Then
  Label1.Text = "you entered..."
Else
 Label1.Text = "sorry, ...
End If
End Sub

(if the list can be very large, maybe 10k+ items, then you should use B4XSet from B4XCollections)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
For B4J Console (Non-UI)
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private reader As TextReader
End Sub

Sub AppStart (Args() As String)
    InitReader
    Dim MyList As List = Array(24, 53, 76)
    Log("Enter a number to check if it is a lucky number")
    Dim Num As Int = ReadLine
    If MyList.IndexOf(Num) > -1 Then
        Log("You entered a lucky number")
    Else
        Log("Sorry, try again later")
    End If
End Sub

Sub InitReader
    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    reader.Initialize(sys.GetField("in"))
End Sub

Sub ReadLine As String
    Return reader.ReadLine
End Sub
Note: You need to compile as Release and run the jar in CMD.

1626109691491.png



 

Attachments

  • LuckyNumber.zip
    972 bytes · Views: 124
Upvote 0
B4X is mostly UI based. There is no console in mobile apps.
B4X:
Private Sub Button1_Click
Dim MyList As List = Array(24, 53, 76)
If MyList.IndexOf(EditText1.Text) > -1 Then
  Label1.Text = "you entered..."
Else
Label1.Text = "sorry, ...
End If
End Sub

(if the list can be very large, maybe 10k+ items, then you should use B4XSet from B4XCollections)
Thank you 😊
 
Upvote 0
For B4J Console (Non-UI)
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private reader As TextReader
End Sub

Sub AppStart (Args() As String)
    InitReader
    Dim MyList As List = Array(24, 53, 76)
    Log("Enter a number to check if it is a lucky number")
    Dim Num As Int = ReadLine
    If MyList.IndexOf(Num) > -1 Then
        Log("You entered a lucky number")
    Else
        Log("Sorry, try again later")
    End If
End Sub

Sub InitReader
    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    reader.Initialize(sys.GetField("in"))
End Sub

Sub ReadLine As String
    Return reader.ReadLine
End Sub
Note: You need to compile as Release and run the jar in CMD.

View attachment 116267


Thank you very much @aeric😊👍🏾
 
Upvote 0
Top