Hi, In Interview I can up with question, How to sum the list by using robot framework keyword

Basic sum of the list program,
sum=0

for i in [1, 2, 3, 4]:

sum=sum+i

print(sum)

Robot framework sum of the list, I’m trying:
*** Settings ***
Library SeleniumLibrary
Library DatabaseLibrary
Library Collections

*** Test Cases ***
Additon of lists
@{list}= Create List 1 2 3 4
${index}= Set Variable 0
FOR ${i} IN ${list}
${index}= Evaluate ${index}+${i}
Log ${index}
END

Error:
Additon of lists | FAIL |
Evaluating expression ‘0+[‘1’, ‘2’, ‘3’, ‘4’]’ failed: TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’

List | FAIL |
1 test, 0 passed, 1 failed

Here is your corrected example (note the expanding symbol @ for the list):

*** Settings ***
Library           SeleniumLibrary
Library           DatabaseLibrary
Library           Collections

*** Test Cases ***
Addition of lists
    @{list}=    Create List    1    2    3    4
    ${index}=    Set Variable    0
    FOR    ${i}    IN    @{list}
        ${index}=    Evaluate    ${index}+${i}
        Log    ${index}
    END

Another (just to show enumeration :wink: ):

*** Test Cases ***
Addition of lists other
    @{list}=    Create List    1    2    3    4
    ${sum}=    Set Variable    0
    FOR    ${index}    ${i}    IN ENUMERATE    @{list}
        ${sum}=    Evaluate    ${sum} + int(${list[${index}]})
        Log    ${sum}
    END

4 Likes

Thanks a lot :slight_smile: