简体   繁体   English

循环内的 for 循环错误 - Python

[英]For-loop inside while-loop error - Python

The following Python code below gives a syntax error on the ':' in the for-loop inside the while-loop.以下 Python 代码在 while 循环内的 for 循环中的 ':' 上给出了语法错误。 Code works fine if for-loop is removed.如果删除 for 循环,代码工作正常。 Have searched this site and Googled around with no answer for this issue.已经搜索了这个网站并在谷歌上搜索了这个问题没有答案。 I am learning Python.我正在学习 Python。 Want to understand where the issue lies here.想了解问题出在哪里。 Thanks.谢谢。

banana = True
orange = 0
print("Test1")
while(orange < 10):
    print("Test Orange %s" % orange)
    for(banana == True):
        print("Test Banana")
        banana = False
    orange += 1

The error is not because of the colon : .错误不是因为冒号: You are using for loop to check if banana == True , The structure of for loop is not used like this.您正在使用 for 循环来检查是否banana == Truefor loop的结构不是这样使用的。 Use while loop instead.改用 while 循环。

while(orange < 10):
    print("Test Orange %s" % orange)
    while(banana == True):
        print("Test Banana")
        banana = False
    orange += 1
for(banana == True):

This line is not correct.这条线是不正确的。 Do you want to use an if?你想使用 if 吗? like this.像这样。

banana = True
orange = 0
print("Test1")
while(orange < 10):
    print("Test Orange %s" % orange)
    if banana == True:
        print("Test Banana")
        banana = False
    orange += 1

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

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