繁体   English   中英

如何在 Python 的嵌套循环函数中限制循环的迭代次数?

[英]How can I limit the number of iteration of a loop in a nesting loop function in Python?

如何在 Python 的嵌套循环函数中限制循环的迭代次数?

代码:

friends= ['Ali', 'Asma','Amna', 'Izma', 'Omer','Zahid','Bilal','Sarah']
for vowelnames in friends:
    if vowelnames in ('Ali','Asma','Izma','Omer'):
        print('Oh my lovely friend ' + vowelnames + '! ,I would like you to come on my graduation ceremony')
    else:
        for cons in friends[:8]:
            if cons in ('Bilal','Sarah','Zahid'):
                print( cons + " I would like you to come on my graduation ceremony")

你必须使用“范围”

friends= ['Ali', 'Asma','Amna', 'Izma', 'Omer','Zahid','Bilal','Sarah']
for i in range(x): #Here instead of x you can give a number so that the loop will run 
                   #till that number
    for vowelnames in friends:
        if vowelnames in ('Ali','Asma','Izma','Omer'):
            print('Oh my lovely friend ' + vowelnames + '! ,I would like you to come 
                  on my graduation ceremony')
else:
    for cons in friends[:8]:
        if cons in ('Bilal','Sarah','Zahid'):
            print( cons + " I would like you to come on my graduation ceremony")

您必须使用“范围”

friends= ['Ali', 'Asma','Amna', 'Izma', 'Omer','Zahid','Bilal','Sarah']
for i in range(x): #Here instead of x you can give a number so that the loop will run 
                   #till that number
    for vowelnames in friends:
        if vowelnames in ('Ali','Asma','Izma','Omer'):
            print('Oh my lovely friend ' + vowelnames + '! ,I would like you to come 
                  on my graduation ceremony')
else:
    for cons in friends[:8]:
        if cons in ('Bilal','Sarah','Zahid'):
            print( cons + " I would like you to come on my graduation ceremony")

这是正确的答案-肯定是!

暂无
暂无

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

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