def divisors(n):
ans =0
for i in range(n):
i += 1
k=n%i
if k!=1:
ans=i
return ans
print(20)
I have a function that's not working properly when I run it prints the n value instead of printing out the divisors.
Three key issues are:
k=n%i returns the remainder - if the remainder != 1 it doesn't mean that i is a divisor!ans and at the end of the function you return the value that was found on the last iteration that satisfied the if condition. What you want to do instead is to accumulate all the anss into a list and return that list. print at the end is not calling the function - it simply prints the number 20.I'm not posting a corrected solution because I think that it will be a good exercise for you to fix these bugs by yourself, good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With