Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ask the user for a number between 3 and 100 (inclusive). USING LOOPS, figure out

ID: 3797408 • Letter: A

Question

Ask the user for a number between 3 and 100 (inclusive). USING LOOPS, figure out what is the largest power of 2 that is less than or equal to the number entered. For example, if the user entered 6, answer would be 4. If the user entered 100, the answer would be 64. You must do this within a function called powerOfTwo. For example: Please enter a number from 3 to 100: 100 The answer is 64 #use the code bellow num = int(input("Please enter a number from 3 to 100: ")) Print(num) if ((num 100)): print("Please follow the directions!") else: answer = power0fTwo(num)$ print("The answer is", answer)

Explanation / Answer

#!/usr/bin/python
def powerOfTwo(num):
   for i in range(num,0,-1):
       if(i & (i-1) == 0):
           res=i
           break
   return res


n=input("enter a number from 3 to 100: ")
print(n)
if((n<3) or (n>100)):
   print("please follow the directions!")
else:
   answer=powerOfTwo(n)
   print("The answer is",answer)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote