Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help! Loops
#1
n = int(sys.argv[1])

power = 1
while 2*power <= n:
    power *= 2
stdio.writeln(power)
    
Hi everyone!I am curious about if i can write this code using for loop and if yes could you give me some hints?


(This program writes,standard output largest power of two less than or equal to n.)
Reply
#2
Quote:Hi everyone!I am curious about if i can write this code using for loop
Yes
Quote:if yes could you give me some hints?
make an attempt, and show where you need help -- hint
while ...:
    ...
Reply
#3
(Apr-15-2020, 07:58 PM)Larz60+ Wrote:
Quote:Hi everyone!I am curious about if i can write this code using for loop
Yes
Quote:if yes could you give me some hints?
make an attempt, and show where you need help -- hint
while ...:
    ...
n = int(sys.argv[1])
p = 1
b = []
for i in range(2,n+1,2):
    b += [i]

a = b[-1]/2
print(a)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020