Nate Nate - 1 year ago 98
Python Question

How to specify argument types?

I'm very much a Python newbie, but I've searched for a solution and I'm stumped.

I have defined a function which accepts several arguments:

def func(arg1, arg2, arg3):

The first argument will be a string but the next two are always going to be integers. I need to construct the following

for x in range(0, arg2 / 2):

The problem is that
is defaulting to type
and as a result I get the following:

TypeError: 'float' object cannot be interpreted as an integer

I have tried:

for x in range(0, int(arg2) / 2):

But the same thing happens for some reason. How can I specify that
should be taken as an integer, or how can I reinterpret it as an integer?

Answer Source

When you arg2 / 2 you will get a float. If you want an int try arg2 // 2

For example 2 / 2 = 1.0 but 2 // 2 = 1

But you will lose accuracy doing this, but since you want an int I'm assuming you want to round up or down anyways.

This is for Python 3.x

In python 2.x / is an int division by default; to get a float division, you had to make sure one of the number was a float.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download