Reputation: 1503
My entire code is below (didn't want to miss anything). For some reason I keep getting an error where I can't convert a float to an int?
import math
def getRange(input):
if (1 <= input < 10):
return [1,1,9]
elif (10 <= input < 190):
return [2,10,99]
elif (190 <= input < 2890):
return [3,100,999]
elif (2890 <= input < 38890):
return [4,1000,9999]
elif (38890 <= input < 488890):
return [5,10000,99999]
elif (488890 <= input < 5888889):
return [6,100000,999999]
def getDigit(input):
workingRange=getRange(input)
multi_digit_dec = ((input-workingRange[1])/workingRange[0])+workingRange[1]
multi_digit_float = math.floor((input-workingRange[1])/workingRange[0])+workingRange[1]
print multi_digit_float
multi_digit_int = input(multi_digit_float)
decimal_remainder = multi_digit_int - multi_digit_dec
## digit_id = decimal_remainder * len(str(multi_digit_int))
## actual_digit = str(multi_digit_dec)[digit_id]
## return actual_digit
getDigit(100)
My error is:
Traceback (most recent call last):
File "C:\Users\Samuel\Desktop\Python\concatenate string of variables and product values.py", line 29, in <module>
getDigit(100)
File "C:\Users\Samuel\Desktop\Python\concatenate string of variables and product values.py", line 22, in getDigit
multi_digit_int = int(multi_digit_float)
TypeError: 'int' object is not callable
>>>
Code updated above to reflect change of variable called int to input
Upvotes: 2
Views: 580
Reputation: 8701
In your stack of elif
statements, if you start by testing if the input number is less than 1, then there's no need to check the lower ends of ranges other than the first. This will cut out half of the verbiage in the elif
's. Also, it is more compact to use a loop for tests like this. For example, the following code produces the output shown below it.
def getRange(k):
if k < 1: return None
e = 1
for d in [10, 190, 2890, 38890, 488890, 5888889]:
if k<d:
return [e, 10**(e-1), 10**e -1]
e += 1
return None
for i in range(14):
print '{:8} {:20}'.format(i * 3**i, getRange(i * 3**i)),
if i&1: print
Output:
0 None 3 [1, 1, 9]
18 [2, 10, 99] 81 [2, 10, 99]
324 [3, 100, 999] 1215 [3, 100, 999]
4374 [4, 1000, 9999] 15309 [4, 1000, 9999]
52488 [5, 10000, 99999] 177147 [5, 10000, 99999]
590490 [6, 100000, 999999] 1948617 [6, 100000, 999999]
6377292 None 20726199 None
Upvotes: 1
Reputation: 123443
The problem with the line multi_digit_int = input(multi_digit_float)
in your updated code is that the argument input
in def getDigit(input):
is hiding the built-in input()
function. Since you calle getDigit
with an argument of 100
, inside the function input
is an int
instance so the input(multi_digit_float)
part is being interpreted as 100(multi_digit_float)
, i.e. an int
calling something.
Upvotes: 0
Reputation: 47267
I think you want:
multi_digit_int = math.floor(multi_digit_float)
Also, don't use int
as a variable name.
Upvotes: 0
Reputation: 500257
The problem is that you're using int
as a variable name, and that shadows the built-in function. Rename the variable.
In general, it's worth familiarizing oneself with the names of the built-in functions, to avoid this type of problems.
Upvotes: 6