I am such a noob at this...
beanthexenoxide (1)

Pls can someone tell me a loop code for python code? Thank you!

You are viewing a single comment. View All
OwenBradstreet (81)

There are two types of loop: Definite and Indefinite.
Definite loops have a certain ending - they will definitely end at some point.
Indefinite loops can end, but don't necessarily have to.

The while loop will execute some code over and over again indefinitely while a condition is met. For example:

x = 0
while (x < 10):
    #Do some things here
    x += 1

This will execute the code 11 times, because x starts at zero and increments by one each time, and while loop will loop indefinitely until x isn't less than 10. Another example code be:

while (True):
     # Doing some cool things forever here
     if (youwantto):
          break

This code will run forever, unless the loop is broken from the inside using break.

The For loop will take a list and execute the code for each item in that list. For example:

names = ["Owen Bradstreet", "Taya Fitzgerald", "Jerry Smith", "John Doe"]
for name in names:
     print(name)

This code will go through every item in the "names" list, replace the temporary variable "name" with the current name it's on and print out that name. The for loop is often used to run some code a certain amount of time like this:

for i in range(0, 9):
     # Cool code here

This will execute that code 10 times.

Hope this helped!
Owen

PS: If this did help you, can you please leave an upvote :) This took me a while to write.