← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #271443]: How can I input a number and accept it as an integer in sikuli?

 

Question #271443 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/271443

    Status: Open => Answered

RaiMan proposed the following answer:
ok, the principal approach is ok.

the return value of input() is a string. Python has a feature, to convert a string into a number:
num = int(str)

If the entered string represents a valid number, the x will contain this
number, if not, the script will crash with an exception (which in turn
can be handled).

For your case this would be the solution base:

xs = "" # the number as String
while xs == "":
    try:
        xs =input("Input 1st Number:")
        xn = int(xs) # check wether xs contains a valid number
    except:
        xs = "" # reset xs in case of not valid and repeat

now you need a sequence, to enter the number having n digits:

for digit in xs: # step through the digits in the string left to right
    if digit == "1":
        click("digit1.png")
   elif digit == "2":
        click("digit2.png")
    ...
    else:
        click("digit0.png")

... using if ... elif ... else is more efficient in this case, where
only one case is valid among the possible 10 cases, since max 9 cases
will be evaluated and the evaluation will end with the first success.

Since you need this sequence for 2 numbers, you could put it in a
function:

def enterNumber(number):
    for digit in number: # step through the digits in the given string left to right
        if digit == "1":
            click("digit1.png")
        elif digit == "2":
            click("digit2.png")
        ...
        else:
            click("digit0.png")

... and the number input we add to a def also:

def enterNumber(text):
    number = "" # the number as String
    while number == "":
        try:
            number =input(text)
            int(number) # check wether number contains a valid number
        except:
            number = "" # reset number in case of not valid and repeat

    for digit in number: # step through the digits in number left to right
        if digit == "1":
            click("digit1.png")
        elif digit == "2":
            click("digit2.png")
        ...
        else:
            click("digit0.png")


now your script will look like this:

def enterNumber(text):
    number = "" # the number as String
    while number == "":
        try:
            number =input(text)
            int(number) # check wether number contains a valid number
        except:
            number = "" # reset number in case of not valid and repeat

    for digit in number: # step through the digits in number left to right
        if digit == "1":
            click("digit1.png")
        elif digit == "2":
            click("digit2.png")
        ...
        else:
            click("digit0.png")

enterNumber("Input 1st Number:")

ops = "+-*/"
while not op in ops:
    op= input("choose operation to be used: + - * /")

# operation to be used + - * / 
if op == "+":
    click("plus.png")
elif op == "-":
    click("minus.png")
elif op == "*":
    click("mul.png")
else:
    click("div.png")

enterNumber("Input 2nd Number:")

click("equal.png")

If you decide to do more complex stuff with SikuliX, you should learn some Python basics:
http://sikulix-2014.readthedocs.org/en/latest/index.html

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.