Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将代码从FOR循环转换为WHILE循环_Python 3.x_Loops_For Loop_While Loop - Fatal编程技术网

Python 3.x 将代码从FOR循环转换为WHILE循环

Python 3.x 将代码从FOR循环转换为WHILE循环,python-3.x,loops,for-loop,while-loop,Python 3.x,Loops,For Loop,While Loop,至 num=int(输入(“输入您的号码”)) i=1 j=1 而j我认为这将实现你想要的: 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-

num=int(输入(“输入您的号码”))
i=1
j=1

而j我认为这将实现你想要的:

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
num=int(输入(“输入您的号码”))
i=1

而i
j
的意义是什么?
j
while
循环中不会递增,因此它永远不会满足您的条件。我认为
i
值在进入循环时会发生变化。因此使用
j
。无法找到增加
i
值的方法。好的,我想我明白了。您需要在循环中增加
j
,并且需要使用
j+=1
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
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