hello, pls can somebody show me a way how to solve the following exercise:
A number, a, is a power of b if it is divisible by b and a/b is a power of b.
Write a function called is_power that takes parameters a and b and returns True if a is a power of b.

I don't exactly understand the task.
I have to use just numbers which meet the conditions above? for examle if I use 3 is power of 4, the result should be false?

thank you very much

I'm not sure what your asking. Do you mean if a = 1000 and b = 10
then a/b which is 1000/10 = 100

So 10 divides 1000 equally..

Is this what you mean?

You want me to explain something to you that I don't know and you don't understand...

Since this is a homework question I can't answer it but I can give you a really big hint – try looking up the modulus operator, its used for this very type of question..

## All 12 Replies

I'm not sure what your asking. Do you mean if a = 1000 and b = 10
then a/b which is 1000/10 = 100

So 10 divides 1000 equally..

Is this what you mean?

I'm not sue what your asking. Do you mean if a = 1000 and b = 10
then a/b which is 1000/10 = 100

So 10 divides 1000 equally..

Is this what you mean?

To be honest, I don' t understand the task. There is a condition which is confusing for me. May be you can explain it to me better.
Before I write the python script I need to understand the result.

Thank you

You want me to explain something to you that I don't know and you don't understand...

You want me to explain something to you that I don't know and you don't understand...

ok, sorry for confusion, the task was mentioned in the first thread:

Exercise 6.7 A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a
function called is_power that takes parameters a and b and returns True if a is a power of b.

I am trying to solve this exercise but I don' t understand the task exactly, it seems that I should use just even number to fulfil condition. for if I use 4 is power if 3 it doesn't match the condition so the result should be false. I understand it in this way.

Since this is a homework question I can't answer it but I can give you a really big hint – try looking up the modulus operator, its used for this very type of question..

Just in case your a complete rookie, the modulus operator is used like this

``````print (34 % 3)
print (32 % 8)
print (41 % 8)``````

where % is the modulus operator...Hope this helps

Oh, I get it.

If the power of a is equal to be, return true else, return false.

``````def is_power(a, b):
power = pow(a)
if power == b:
return True
else:
return False``````

I usually don't do assignments, but because know one seemed to know what it was I gave it. But here's what's happening, parameters for functions are pretty much variables between the parenthesis, so when you type the function you would put the value of the variables in parenthesis, so "is_power(2, 4)" would set 2 to the variable a, and set 4 to the variable b. Now Return simply sends the outcome back to the session. Make sure that True and False are capitalized, otherwise it will think they are variables. But if you ran this program like this, "is_power(2, 4)", after execution it would print True on the screen since the power of 2 is 4. Hope that helped, add it to my rep. if it really helped.

Just to help you to understand what you have to do :
a is a power of b if it is divisible by b and a/b is a power of b :
a is power b => a/b is power of b :
example :
1000 is power of 10 ?
1000 / 10 = 100
is 100 a power of 10 ?
100 / 10 = 10 which is, obviously, a power of 10 so 100 is a power of 10, so 1000 is a power of 10.

Is 3 a power of 4 ?
3 / 4 = 0.75 which is not a power of 4.

note :
10 power 3 = 10 * 10 * 10
5 power 6 = 5 * 5 * 5 * 5 * 5 * 5

Since this is a homework question I can't answer it but I can give you a really big hint – try looking up the modulus operator, its used for this very type of question..

yes, I don' t want the answer, I want to find solution by myself. Sorry iI hat to leave for a while and now I can see that you gave me whole answer. I mean , I am not look at it!
I can make a script that should not be problem, I was just curious how you understand the task.
for me the first condition "a, is a power of b if it is divisible be b and a/b is power of b" these are actually the conditions.
for example if I take a = 4 and b = 3 , the condition is not fulfiled and return should be 'false', another example a = 2, b = 4 , a is divisible be b which is ok, but a/b (2/4) is power of b(4), this I don't understand, 1/2 is not power of 4.

note :
10 power 3 = 10 * 10 * 10
5 power 6 = 5 * 5 * 5 * 5 * 5 * 5

ok, that I understand.

Just to help you to understand what you have to do :
a is a power of b if it is divisible by b and a/b is a power of b :
a is power b => a/b is power of b :
example :
1000 is power of 10 ?
1000 / 10 = 100
is 100 a power of 10 ?
100 / 10 = 10 which is, obviously, a power of 10 so 100 is a power of 10, so 1000 is a power of 10.

Is 3 a power of 4 ?
3 / 4 = 0.75 which is not a power of 4.

I think I was looking for this kind of help. Thank you very much! Now I understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.