0

Need help plz! Kaprekar numbers, while loops, and if/else statements

Hi guys! I need to code a program that returns True or False whether the number entered is a Kaprekar number or not. Example: 9 is kaprekar number because 9 * 9 = 81 and 8+1 = 9. 45 is kaprekar too because 45 * 45 = 2025 and 20+25= 45. The secret here is the digit break down. I used divmod to do it and so far it's good. The problem is: when I try with a kaprekar number, it returns True, but when I try any other number, it says "no output". Here's the code I came up with: https://code.sololearn.com/c2XZHX7x28qs/?ref=app

5th Mar 2021, 9:42 PM
Cédrik Roberge
Cédrik Roberge - avatar
4 Antworten
0
Oh and also, I think it may be because of the if/else statements that there's no output. Still looking into it, but if anyone can help, it would be great!
5th Mar 2021, 9:43 PM
Cédrik Roberge
Cédrik Roberge - avatar
0
Maybe this will help you, def is_kaprekar_number(num): if num == 1: return True d = 10 kap = num ** 2 if sum(divmod(kap,d)) == num: print("hy") return True elif sum(divmod(kap,d)) != num: while not sum(divmod(kap,d)) == num: d = d * 10 somme = sum(divmod(kap,d)) if somme == num: return True elif divmod(kap,d)[1]>num: return False else: continue print(is_kaprekar_number(46)
5th Mar 2021, 10:10 PM
Abhay
Abhay - avatar
0
Thanks a lot everyone 🙏🏼🙏🏼
6th Mar 2021, 3:27 PM
Cédrik Roberge
Cédrik Roberge - avatar