How can I input a number and accept it as an integer in sikuli?

Asked by Jenelle

x = input("Input a number")
I want to let x accept an integer, not a string.
please help me

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Eugene S (shragovich) said :
#1

this has nothing to do with Sikuli but...

input doesn't have the ability to check the input type. You will have to process it yourself. Below is a trivial example:

while True:
    if type(input("Input a number")) is int:
        print "That's an integer"
        break
    else:
        print "That's not an integer. Enter integer!"

Revision history for this message
Jenelle (ma-jenelle-casanas) said :
#2

I need a number inputted by the user to automate in my calculator
Do you have any idea how to do it?
I want to make an automation where sikuli will ask the user to give two(2) integers/number and sikuli will do the rest to make it automated in inputing in calculator.
please help :'(

Revision history for this message
Jenelle (ma-jenelle-casanas) said :
#3

I need a number inputted by the user to automate in my calculator
Do you have any idea how to do it?
I want to make an automation where sikuli will ask the user to give two(2) integers/number and sikuli will do the rest to make it automated in inputing in calculator.
please help :'(

Revision history for this message
Eugene S (shragovich) said :
#4

what's wrong with the solution I offered?

Revision history for this message
masuo (masuo-ohara) said :
#5

If you want to convert a string to an integer , use the function (for example, isdecimal()).
--------------------
errmsg = ""
while True:
    msg = "Input a number\n" + errmsg
    ans = input(msg)
    if ans == None:
        cancel = True
        break
    if ans.isdecimal():
        x = int(ans)
        cancel = False
        break
    else:
        errmsg = "'" + ans +"'" + " is not a number"

Revision history for this message
masuo (masuo-ohara) said :
#6

sorry:
If you want to evaluate the input value, use the function (for example, isdecimal()).
If you want to convert a string to an integer, use the function (for example, int()).

Revision history for this message
Jenelle (ma-jenelle-casanas) said :
#7

x= input("Input 1st Number:")
y= input("Input 2nd Number:")
op= input("choose operation to be used: + - * /")

if x == "1":
    click("1442372005556.png")
if x == "2":
    click("1442372023449.png")
if x == "3":
    click("1442372434642.png")
if x == "4":
    click("1442372451565.png")
if x == "5":
    click("1442372460538.png")
if x == "6":
    click("1442372475324.png")
if x == "7":
    click("1442372486937.png")
if x == "8":
    click("1442372506844.png")
if x == "9":
    click("1442372515627.png")
if x == "0":
    click("1442372522700.png")

/*opeationr to be used + - * / */
if op == "+":
    click("1442457788556.png")
if op == "-":
    click("1442457811305.png")
if op == "*":
    click("1442457824868.png")
if op == "/":
    click("1442457838430.png")

if y == "1":
    click("1442372005556.png")
if y == "2":
    click("1442372023449.png")
if y == "3":
    click("1442372434642.png")
if y == "4":
    click("1442372451565.png")
if y == "5":
    click("1442372460538.png")
if y == "6":
    click("1442372475324.png")
if y == "7":
    click("1442372486937.png")
if y == "8":
    click("1442372506844.png")
if y == "9":
    click("1442372515627.png")
if y == "0":
    click("1442372522700.png")

click("1442457857653.png") /*equal sign image

----so this is my codes looks like. And my problem here now is... I can't manipulate a number that composed of more than 1 digit :( Im so sorry, this is my first time using sikuli. I can't even find answers in google. :'( Im so desperate right now. tho I really want to learn this thing. thank you

Revision history for this message
RaiMan (raimund-hocke) said :
#8

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

Revision history for this message
RaiMan (raimund-hocke) said :
#9

ok, I just realized that masuo already mentioned the int() function and I have to admit, that I was not aware of isdecimal().
But since this only works on unicode strings, I still would use int() in this case.

Can you help with this problem?

Provide an answer of your own, or ask Jenelle for more information if necessary.

To post a message you must log in.