def kaprekar_num(num):
count = 0
while count <= num:
n = 1
sqr = n ** 2
digits = str(sqr)
length = len(digits)
for x in range(1, length):
left = int("".join(digits[:x]))
right = int("".join(digits[x:]))
if (left + right) == n:
print("Number: " + str(n) + ", Left: " + str(left) + " + " + " Right: " + str(right) + " = " + str(n))
n += 1
count += 1
else:
n += 1
kaprekar_num(5)
hello guys,
I'm new to python programming and I got a task in class to print the first 5 kaprekar numbers.
(I only have C programming background...)
I have a problem with the "for x in range..." line.. the code doesn't enter the loop and I don't know why.
the program needs to print:
Number: 9, Left: 8 + Right: 1 = 9
Number: 10, Left: 10 + Right: 0 = 10
Number: 45, Left: 20 + Right: 25 = 45
Number: 55, Left: 30 + Right: 25 = 55
Number: 99, Left: 98 + Right: 1 = 99
I will appreciate some insights :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…