You are not logged in.

#1 2005-07-20 09:14:00

johnisevil
Member
From: Hamilton, ON Canada
Registered: 2003-08-07
Posts: 221
Website

Python math question

Any ideas as to why the last line always equals 0 when it shouldn't?

tmp = os.popen('free -m').readlines()
total = int(tmp[1].split()[1])
used = int(tmp[2].split()[2])
free = int(tmp[2].split()[3])
pused = (int(used) / int(free)) * 100

print pused

Offline

#2 2005-07-20 09:55:11

bleak
Member
Registered: 2004-05-11
Posts: 24

Re: Python math question

That's because you're doing an integer division. Do this instead:

pused = float(used) / free * 100

Offline

#3 2005-07-20 09:57:39

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: Python math question

Looks like you have more free space than used space, you should be happy smile

The division of 2 integers is an integer. The division is always less than 1 in your case, and so the integer which represents it is 0.

Offline

#4 2005-07-20 09:58:43

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: Python math question

Ah, you were quicker smile

Offline

#5 2005-07-20 10:03:02

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: Python math question

btw. if you want to do an integer division, you don't have to use int on both operands, just use //.
a = 2.031
b = 3.5 * a
b // a

Offline

Board footer

Powered by FluxBB