有没有办法在不进入下一个索引的情况下继续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"]
答案 0 :(得分:1)
for
循环旨在允许您迭代每个元素一次,而不必担心如何实际移动到下一个索引。
for each player:
# do something
如果您需要更多控制何时或是否要移动到下一个索引,可以使用while
循环。
while (some_condition_is_true):
# do something