0

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 loop.

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

The problem is that arg2 is defaulting to type float 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 arg2 should be taken as an integer, or how can I reinterpret it as an integer?

martineau
  • 119,623
  • 25
  • 170
  • 301
Nate
  • 105
  • 1
  • 12

2 Answers2

3

In Python 3.x:

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.

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.

Edited: As @martineau pointed out, in both Python 2.x and 3.x if the numerator or denominator is a float, / will do float division and // will result in a float result. To get around this cast it to int or make sure it's an int... 2.0 / 2 = 1.0 and 2.0 // 2 = 1.0 and 2 / 2.0 = 1.0 and 2 // 2.0 = 1.0 2.5 / 2 = 1.25 and 2.0 // 2 = 1.0 and 2 / 2.5 = 0.8 and 2 // 2.5 = 0.0

MooingRawr
  • 4,901
  • 3
  • 24
  • 31
  • Thank you - could you just briefly explain the difference between / and //? I have coding experience but not in python. – Nate Sep 02 '16 at 14:12
  • @Nate // is Floor Division – galaxyan Sep 02 '16 at 14:13
  • @Nate You can find the answer here: http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division But tl dr / is a floating point division and // is a int division. Basically // forces you an int instead of a general float. – MooingRawr Sep 02 '16 at 14:13
  • I believe 2 / 2 = 1.0 is not true in python 2 – galaxyan Sep 02 '16 at 14:14
  • That makes perfect sense, thanks everyone. Is there a "best" python documentation center that is recommended? I'm used to Mathematica, which has really thorough and fantastic in-program documentation. Python seems a little more variable. – Nate Sep 02 '16 at 14:16
  • @Nate Stackoverflow just started a documentation center. It's being updated and added by the community every day, check it out. – MooingRawr Sep 02 '16 at 14:17
  • @galaxyan you are correct. Back in Python 2.x / was an int division, and to get a float division you had to make sure one of the variables was a float. In Python 3.x they changed it so you had more power in / and //. I'm not sure the exact reasoning for this, but I found it more useful now. – MooingRawr Sep 02 '16 at 14:18
  • @MooingRawr Then do mention that you are using python version 3 because if you only say python even in tags people will think of 2. :) – maharshi Sep 02 '16 at 14:24
  • `//` does integer division as you say, but if the `arg2` argument was passed to the function as a float value, say `5.0`, the result of the division will be a float, too. In other words, `5.0 // 2` results in `2.0` in both Python 2 & 3. – martineau Sep 02 '16 at 15:35
  • @martineau, huh, I've never noticed that. sweet.... I will update my answer to include that. Thanks for teaching me something new. – MooingRawr Sep 02 '16 at 15:56
  • Still not quite right. Using `//` doesn't result in float division, just a float result. A less problematic approach might be do just do a `arg2 = int(arg2)`, or `int(round(arg2))`. – martineau Sep 02 '16 at 16:48
0

try to round for whole formula

for x in range(int(arg2/2)) 
galaxyan
  • 5,944
  • 2
  • 19
  • 43