簡體   English   中英

如何繼續for循環而不轉到下一個索引? -Python

[英]How do I continue a for loop without going to the next index? - 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"]

for循環旨在使您可以遍歷每個元素一次,而不必擔心如何實際移至下一個索引。

for each player:
   # do something

如果您需要更多控制時間,或者是否希望移至下一個索引,則可以使用while循環。

while (some_condition_is_true):
   # do something

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM