繁体   English   中英

Python递减for循环内的变量

[英]Python decrement the variable inside for loop

我将我的 java 代码转换为 python 代码,以及如何在 python 中减少 for 循环内的变量? 如果它在 if 语句中,我尝试将索引减少 1,但显然我不能这样做。 有没有其他方法可以在 for 循环中减少 i ?

Java代码:

for(int i = 1; i <= 3; i++)
        {
            System.out.print("Enter Movie " + i + " of " + 3 + " : ");
            String inputMovie = sc.nextLine();
            if (inputMovie.equals("")) 
            {
                System.out.println("Please input a movie name.");
                System.out.println("");
                i--;
            }

            else
                movies.offer("'"+inputMovie+"'");
        }

蟒蛇代码:

for i in range(1,4):
    inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
    if inputMovie=="":
        print("Please input a movie name")
        print("")
        i-=1
        pass
    else:
        movies.append(inputMovie)
    pass

输出:好吧,如果我们查看输出,它仍然在递增而不是递减 i

Enter Movie 1 of 3 :
Please input a movie name

Enter Movie 2 of 3 :
Please input a movie name

Enter Movie 3 of 3 :
Please input a movie name

Python 不允许您在for循环中更改迭代器。 一旦循环的下一次迭代到来,迭代器就会成为可迭代对象的下一个值。

这也是因为range行为不像真正的 Java 类for循环。 相反,它会不断生成范围内的数字(您可以通过在 Python 解释器中键入list(range(10))来查看这一点,它将生成一个从 0 到 9 的数字列表。

如果你想修改迭代器,你应该使用while循环来代替:

i = 1
while i <= 3:
    inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
    if inputMovie=="":
        print("Please input a movie name")
        print("")
        i-=1
    else:
        movies.append(inputMovie)
    i = i + 1

这应该与您的 Java 代码相同,因为我只是将三个指令从 Java for循环移动到它们的位置。 不需要通知pass因为它是一个没有效果的声明。

为了优化,让我说你真的不需要减少迭代器,只是避免增加它 我将此解决方案与原始答案分开,因为它与您的原始设计存在重大偏差:

i = 1
while i <= 3:
    inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
    if inputMovie=="":
        print("Please input a movie name")
        print("")
    else:
        movies.append(inputMovie)
        i = i + 1

我所做的就是删除减量并将增量推送到else块,因此它仅在输入电影名称时运行。

你应该使用while语句

“不幸的是” for循环将保留“内存”并在每次迭代时重新分配给下一个值

i = 1
while i < 4:
    inputMovie = input("Enter Movie " + str(i) + " of " + str(3) + " : ")
    if inputMovie == "":
        print("Please input a movie name")
        print("")
        i-=1
    else:
        movies.append(inputMovie)
        i+=1

pass指令无关紧要,你可以省略

通过语句

range(low,high)生成一个由从low开始到high-1结束的元素组成的序列。 这就是为什么您的i-=1不起作用,因为我正在该列表中进行迭代。
这里最简单的替代方法是使用while循环。

while i<target:
    if something:
        #do something
        i += 1

Python 中的 for 循环更像是 for-each。 因此,无论循环中的更改/更新如何,循环值(i)都将更新为下一个值。

更好的方法是使用 while 循环。

i = 1
while i <= 3:
    inputMovie = input("Enter Movie " + str(i) + " of " + str(3) + " : ")
    if inputMovie=="":
        print("Please input a movie name")
        print("")
        i-=1
        pass
    else:
        movies.append(inputMovie)
        i+=1
    pass

您必须正确设置range()函数。 为了减少循环,您可以使用while循环,或者您可以更改算法并设置for循环,但现在您可以做的是将范围函数步长值选择为 -1。 请尝试检查代码,因为我也和你一样有同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM