如何在不转到下一个索引的情况下继续for循环?Python

2024-09-22 14:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有什么方法可以继续for循环而不转到下一个索引

示例:

for player in range(players):
    name = input("what is your name? ").lower()
    if name == "john cena":
        print("Hey! That's not your real name!")
        [some keyword or function that allows the magic to happen]
    elif name == "donald trump":
        print("Hi Mr. President!")
        [the same keyword or function that allows the magic to happen]

    player_names.append(name)

外壳:

============ RESTART =========

what is your name? luke
what is your name? john cena
Hey! That's not your real name!
what is your name? donald trump
Hi Mr. President!
what is your name? mickey mouse
what is your name? nobody
>>> player_names
["luke", "mickey mouse", "nobody"]

Tags: thenameforyourthatisnotjohn
1条回答
网友
1楼 · 发布于 2024-09-22 14:39:40

for循环允许您对每个元素迭代一次,而不必担心如何实际移动到下一个索引

for each player:
   # do something

如果您需要更多地控制何时或是否要移动到下一个索引,可以使用while循环

while (some_condition_is_true):
   # do something

相关问题 更多 >