0
num = int(input("Enter your number: "))

for i in range (1, num+1):
    if (i%2==0):
        i = i // 2
        print(3**(i-1))
    else:
        i = i // 2 + 1
        print(2**(i-1))

TO

num = int(input("Enter your number: "))

i=1
j=1
while j<=num and i<=num:
    if (i%2==0):
        i = i // 2
        print(3**(i-1))
    else:
        i = i // 2 + 1
        print(2**(i-1))
j+1

Facing problem in while loop conversion.
Searched this problem everywhere on the internet but unable to solve it.

4
  • what is the point of j? Commented Dec 28, 2020 at 4:36
  • j is not being incremented within the while loop, so it will never satisfy your conditions. Commented Dec 28, 2020 at 4:40
  • I think i value get changed when it enters inside the loop. So used j. Not able to find a way of incrementing the value of i. Commented Dec 28, 2020 at 4:42
  • Ok, I think I understand. You need to increment j within the loop and you will need to use j += 1 Commented Dec 28, 2020 at 4:45

1 Answer 1

1

I think this will accomplish what you want:

num = int(input("Enter your number: "))

i = 1
while i <= num:
    if (i % 2 == 0):
        print(3 ** ((i // 2) - 1))
    else:
        print(2 ** (i // 2))
    i += 1

i will increment by 1 after each loop. Which mimics for i in range(1, num + 1). i is not modified within the loop, it is only used in the printed calculation.

Note: in your for loop, your modifications to i do not affect the i variable for the loop but I would suggest that you try to avoid using the same variable name within the loop.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.