Reputation: 13
I have the following code, which keeps producing errors:
import math
def pen_checker(number):
print(number)
for x in range(1, number):
y = x*(3*x-1)/2
if(number == y):
return True
return False
def pen_calculator(n):
x = n*(3*n-1)/2
return x
def main():
pen1 = 1
pen2 = 1
pen1_val = 0
pen2_val = 0
crt_sum = 0
crt_dif = 0
MAX_CAP = 1000
for pen1 in range(1, MAX_CAP):
pen1_val = pen_calculator(pen1)
for pen2 in range(1, MAX_CAP):
pen2_val = pen_calculator(pen2)
z = pen1_val + pen2_val
if(pen_checker(z)== True and pen_checker(fabs(pen1_val-pen2_val))== True):
print(fabs((pen1_val-pen2_val)))
main()
For some reason, the function pen_calculator()
seems to return floats. I know that technically, there are no variable types in Python, but before I call the function, everything is printed as:
1
1
2
And afterwards:
1.0
1.0
2.0
(That was just me trying to find out what's wrong, it is not written in the code)
Normally, this would be no problem, but a for-loop in the function pen_checker
requires the "stop" to be an integer, so the code won't work. How do I solve this?
Upvotes: 0
Views: 96
Reputation: 59112
In Python3, if you divide numbers with /
it will always give you a float. If you want to divide integers and get an integer, you can use the //
operator.
Upvotes: 5