首先,它在 while 循环条件下给我语法错误。为了消除这里的零除法错误,我 已将条件赋予 while 循环,以便数字不等于零。现在,我不知道该怎么办?
def is_power_of_two(number):
while (number!== 0 && number%2!=0):
number = number / 2
if number == 1:
return True
else
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
我试图解决它,但没有成功。
正如我所看到的,有一个语法错误,您在 else 之后错过了“:”。
此外,我在 while 循环中更改条件。现在它给出了你提到的答案
def is_power_of_two(number):
while (number > 1) & (number % 2 == 0):
number = number / 2
if number == 1:
return True
else:
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
假
真
真
假的
• 我正在尝试在 Visual Studio 中使用角度项目在点网中调用 API,但它给出了 404 未找到错误 [关闭]
• 如何在 PHP 中验证字符串中的前 2 个字母或数字? [重复]
• 为什么我的内联样式会从我的 vue.js 应用程序中删除?
• “tuple[str, str]”类型的参数不能分配给“tuple[Literal, Literal]”类型的参数“__key”
• 使用 DistributedDataParallel 转换模型时没有任何输出挂断
• 如何使用 Twitter Bootstrap 将 Glyphicons 水平居中
• Invalid column count in CSV input on line 1 error (Already checked other questions)