-4

I need to return if the given number is the power of two or not

def PowersofTwo(num):
 i = int
 for num in I:
   if num == 2**[I]:
     return ("true")
   else:
     return ("false")

print PowersofTwo(raw_input())

I need some guidance on how to revise it

  • ```import numpy as np``` ```if np.log2(num) == int(np.log2(num)): return True``` ```else: return False``` – Nin17 Jun 02 '22 at 21:21
  • Please pick a more descriptive title and explain better what problem you face; include any error messages in your question. See [ask]. Quick guess: `print` needs parens in Python3: `print(...)`. `raw_input` should be just `input`. Python is case-sensitive, so `i` and `I` are different variables. `int` is a reserved word; not sure what you are trying to do here. You should probably get a Python tutorial. – Robert Jun 02 '22 at 21:22
  • How many iterations do you think your for loop goes through if you return in both the if and else? – Sayse Jun 02 '22 at 21:23
  • Does this answer your question? [How can I test whether a number is a power of 2?](https://stackoverflow.com/questions/108318/how-can-i-test-whether-a-number-is-a-power-of-2) – Programmer Jun 02 '22 at 21:24
  • This question is already answered here: https://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2 – BhusalC_Bipin Jun 02 '22 at 21:25
  • This question is already answered here: – BhusalC_Bipin Jun 02 '22 at 21:26

0 Answers0