[Tutor] Code

ThreeBlindQuarks threesomequarks at proton.me
Thu Jul 13 16:13:38 EDT 2023


Hari,

We can help better when we understand the problem and can read and run the code. As Dennis pointed out, you code is not right for any purpose.

Before writing code it can be helpful to write down the algorithm you want to use. What you show makes no sense to me. 

The goal seems to be to check if a number supplied is a power of two. Besides 2,4,8,16,... do you want to accept two raised to the zero power (1) or negative powers?

So what is your algorithm? How does your code reflect it?

Your code shown starts with:

def is _power_off_two(number)

Suggestion, a function definition ends with a colon and usually carriage return.

In English, the words "off" and "of" mean different things. It is perfectly legal to call your function is_power_off_two but not if you have a space after "if".

Clearly the code you are showing us is NOT what you told python as it will not run here and you sow sample output.

Your next line floors me:

number=2

This HAS TO be indented. And it makes no sense to change what is an input variable to your function as once changed, it will now do the same thing every time you call it with any number.

Your next line is also nonsense. It too needs indentation and is not valid code:

while number ? 2==0:

What is the purpose of the question mark? Do you mean to use whatever symbols it takes to ask if the number modulo 2 returns 0 as a remainder? I am guessing that is what you want and you need the right way to do that.

You then divide the number by 2. THe line is not indented. Depending on how you indent, it can be part of the while loop or back in the main body of the function or ???

number = number/2

And note you asked for regular division rather than integer division so 3/2 can be 1.5.

Then you have an IF statement badly formatted. Is it inside the while or not? Normally I expect an ELSE clause but in your case, it looks like you have an unconditional False return if the code is reached which is valid.

> If number == 1:
>    return true
>  return False

You then ask why it does not work? We have no idea as the code you show us cannot work at all. 

But looking at your examples, if this is for a class as an assignment, you seem to consider valid powers of two to include 1 and 2 and 4 and so on.

I do not see you showing what did happen, just what you expect. So I am guessing your question is not why the algorithm failed but why the code was not accepted at all by Python. So why not supply the error messages received. Consider fixing some of the things Dennis and I are talking about like not having a space after is and having a colon at the end of the line, and see what breaks next.

Then consider what happens in your code if a number comes in that makes the while loop run forever and so on.

Good Luck.













Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, July 13th, 2023 at 9:13 AM, hari ganivada <hariganivada81 at gmail.com> wrote:


> Sir
> 
> I am hari Ganivada.
> def is _power_off_two(number)
> number=2
> while number ℅ 2==0:
> number = number/2
> If number == 1:
> return true
> return False
> Call to the function
> 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.
> 
> Please help me sir
> 
> 
> 
> ---------- Forwarded message ---------
> From: hari ganivada hariganivada81 at gmail.com
> 
> Date: Thu, 13 Jul, 2023, 18:41
> Subject:
> To: tutor at python.org
> 
> 
> 
> Sir
> 
> I am hari Ganivada.
> def is _power_off_two(number)
> number=2
> while number ℅ 2==0:
> number = number/2
> If number == 1:
> return true
> return False
> Call to the function
> 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.
> 
> Please help me sir
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list