Simple Math... not simple in Python?
#1
i have a simple way in vb to convert any number into a integer multiple of 10

v = int(v/10)*10

but this does not work in python.

the error tells me that the '/' is not allowed.

so i tried int((v/10)) but no workie.

help please!

:nuts:
I'm not an expert but I play one at work.
Reply
#2
/ is correct for division.

maybe your indentation might be off or maybe v isn't declared before the formula.

should work fine.

i just did the following and works fine.

Quote:v = 1034
v = int(v/10)*10
print v

result = 1030



For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#3
ok, maybe it is a string conversion thing, i'm not sure. in vb it is a variant so it does not matter. but python is much more picky... it is getting on my nerves actually.

goal: input a number, do some math, output a string using the math's output.

when i try it on xbmc i get this log...


info typeerror
info :
info unsupported operand type(s) for /: 'str' and 'int'


Quote:def getdimlevel(type, v): # types: 1=image, 2=text
if type == 1:
v = "dims" & str(int(v/10)*10) & ".gif"
elif type == 2:
v = str(int(v/10)*10) & "%"
return v
I'm not an expert but I play one at work.
Reply
#4
it is saying that you can't divide a str by and int.
the parameter you are passing in for v is a str.
if v is something like '100' (a string that looks like a number) do

v = "dims" + str(int(parseint(v)/10)*10) + ".gif"

i don't know what & does to strings but you probably want + instead.
Reply
#5
(asteron @ jan. 10 2006,09:41 Wrote:it is saying that you can't divide a str by and int.
the parameter you are passing in for v is a str.
if v is something like '100' (a string that looks like a number) do

v = "dims" + str(int(parseint(v)/10)*10) + ".gif"

i don't know what & does to strings but you probably want + instead.
parseint does not seem to be valid?

the goal is to take a number, change it to the 'closest' multiple of 10, then create a filename with it.

example:

input = integer 43
closest multiple of 10 = 40
create filename = dims40.gif

in vb this is so simple, python wants to make it difficult!
I'm not an expert but I play one at work.
Reply
#6
haha sorry i was thinking javascript.

v = "dims" + str((int(v)/10)*10) + ".gif"
will be fine. no need to cast the division back into an int since this is integer arithmetic.
Reply

Logout Mark Read Team Forum Stats Members Help
Simple Math... not simple in Python?0