Sikuli loop with python

Asked by Wei Yang

This is the current code (I am using try/except to handle exceptions) I am using, I would like to know if there are any way to make it LOOP with a python list to shorten the code :

try:

   .
   type("first number in A")
   type("first number in B")
   .

except FindFailed:

   type(Key.F5)

For e.g.

A=[111,222,333]
B=[123.456.789]
try:

   .
   type("first number in A")
   type("first number in B")
   .

except FindFailed:

   type(Key.F5)

**Once the first loop is completed, how to choose the second number from list A and B etc. Please kindly correct me if I have misunderstanding in any ways. Thank you for your time.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Cameron Jones
Solved:
Last query:
Last reply:
Revision history for this message
Cameron Jones (jones-cameron-314) said :
#1

Are A and B both lists with 3 elements? If A and B always have an equal number of elements then you could do:

for i in range(0:len(A)):
    try:
        .
        type(A[i])
        type(B[i])
        .
    except FindFailed:
        type(Key.F5)

However in your example, A has 3 elements, and I have to assume B has one element, since it isn't separated by commas, but instead is connected by a series of decimal points.

Revision history for this message
Wei Yang (antideath2014) said :
#2

Hi Cameron, thanks for your reply.

Both A and B have an equal number of elements. I apologise for the typo in B, it should be commas instead.

I have tried as you suggested but an error message propped up "[error]Syntax Error ("no viable alternative at input ':'")"

Please kindly correct me if I have misunderstandings in any ways.

Thank you.

Revision history for this message
Cameron Jones (jones-cameron-314) said :
#3

Yeah that is my mistake, it should be just 'range(len(A)):'

Revision history for this message
Wei Yang (antideath2014) said :
#4

the first part works, but the second part there is an error message. I wonder what causes the error.

A=[111,222,333]
B=[123.456.789]
for i in range(len(A)):
    try:
        .
        type(A[i]) (line 12)
        type(B[i])
        .
    except FindFailed:
        type(Key.F5)

** [error] Type Error (type(): 1st arg can't be coerced to string)
     [error] --- Traceback --- error source first line: module(function) statement 12 main: (<module>)type(A)
     [error] --- Traceback --- end

Revision history for this message
Best Cameron Jones (jones-cameron-314) said :
#5

Alright, so first off, you still have the type in the assignment of B, and you need to correct that.
"B=[123.456.789]" is incorrect and it should be separated by commas.

Secondly, you can just wrap a str() call around A[i] and you should be alright. Same with B[i].

A=[111,222,333]
B=[123,456,789]
for i in range(len(A)):
    try:
        type(str(A[i]))
        type(str(B[i]))
    except FindFailed:
        type(Key.F5)

Revision history for this message
Wei Yang (antideath2014) said :
#6

It is solved, thanks a lot. Learnt a lot from you Cameron.