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
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!
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)
0
https://code.sololearn.com/cg1zm21Cscai/?ref=app
https://code.sololearn.com/cLqr4gV91thc/?ref=app
https://code.sololearn.com/cynVlRo0laP0/?ref=app
https://code.sololearn.com/cqMKiDZJdtj1/?ref=app
https://code.sololearn.com/cA328eAJdXnQ/?ref=app
https://code.sololearn.com/cArYMY7MWoQV/?ref=app
https://code.sololearn.com/c8J7AerNAt5s/?ref=app
https://code.sololearn.com/cZt7qTvOkV57/?ref=app
https://code.sololearn.com/c9PYx1wnuJ8S/?ref=app
https://code.sololearn.com/c0fyN7p7y5m1/?ref=app
https://code.sololearn.com/ck1896cNQQDA/?ref=app
https://code.sololearn.com/cO8yUwiVvBQ5/?ref=app
https://code.sololearn.com/cgsBlU7fUZF3/?ref=app
https://code.sololearn.com/caV24m5ybPLl/?ref=app
https://code.sololearn.com/cgg0fKn82FmP/?ref=app
https://code.sololearn.com/cMC5yI4nn371/?ref=app
https://code.sololearn.com/cMWal97HJm0j/?ref=app
https://code.sololearn.com/c4gqu5fCbPGR/?ref=app
https://code.sololearn.com/cRK80hygTCey/?ref=app
https://code.sololearn.com/W3ViSJplerA8/?ref=app
https://code.sololearn.com/cDSp1ShQCZOR/?ref
0
Thanks a lot everyone 🙏🏼🙏🏼