Division not working in Sikuli with Python

Asked by Jacco Peters on 2020-02-14

I'm trying to divide 2 numbers in Python. When I do this within Sikuli the results keeps being 0. Whereas when I do this in Pycharm I get the expected result. Can anybody help me please. I'm a bit stuck.

Code:

            tot_words = '9'
            tot_found = '5'

            tot_words=int(tot_words)
            tot_found=int(tot_found)

            if tot_words == 0:
                print('xxxx found_factor = 0')
                found_factor = 0
            else:
                found_factor = tot_found/tot_words
                print('yyyyy found_factor > 0')
                print('tot_found: ' + str(tot_found))
                print('tot_words: ' + str(tot_words))

            print('found_factor1: ' + str(found_factor))

Result in Sikuli IDE NOT as expected

yyyyy found_factor > 0
tot_found: 5
tot_words: 9
found_factor1: 0

Result in Pycharm is as expected:

yyyyy found_factor > 0
tot_found: 5
tot_words: 9
found_factor1: 0.5555555555555556

Process finished with exit code 0

Can somebody explain the difference. That would be very much appreciated

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
2020-02-15
Last query:
2020-02-15
Last reply:
2020-02-14
RaiMan (raimund-hocke) said : #1

not sure, what you are doing:

print int("5")/int("9")
print 5/9
x = "5"
y = "9"
z = int(x)/int(y)
print "zs", z
x = 5
y = 9
z = x/y
print "zi", z

prints

0
0
zs 0
zi 0

Best Manfred Hampl (m-hampl) said : #2

The behavior of integer divisions has changed between python2 and python3.

In python2 dividing an integer by an integer (5/2) results in an integer (=2) (fraction of result cut off).
In python3 dividing an integer by an integer (5/2) results in a float result (=2.5). There is now a//b giving an integer result.

Maybe this is the explanation of the difference.

Jacco Peters (jaccopeters) said : #3

Thanks Manfred Hampl, that solved my question.

Jacco Peters (jaccopeters) said : #4

Thanks for that answers. It was indeed the difference between the python versions

replacing

found_factor = tot_found/tot_words

by

found_factor = tot_found/float(tot_words)

solved it.