user9352669
user9352669

Reputation:

How do I continue a for loop without going to the next index? - Python

is there any way to continue a for loop without going to the next index?

Example:

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)

Shell:

============ 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"]

Upvotes: 0

Views: 78

Answers (1)

MxLDevs
MxLDevs

Reputation: 19506

A for loop is intended to allow you to iterate over each element once, without having to worry about how to actually move to the next index.

for each player:
   # do something

If you require more control over when, or if, you would like to move to the next index, you can use a while loop.

while (some_condition_is_true):
   # do something

Upvotes: 1

Related Questions