Ask coding questions

← Back to all posts
How do I make it loop
NathanWeems (5)

So what I want it to do is to keep doing the loop of the three attacks until the enemy_hp is 0 and then move onto something else. It's in turtle because I'm going to add animations later

Comments
hotnewtop
PYer (4010)

You've already used loops in your project, so I won't have to explain everything about every type of loop, thank goodness. To iterate a select amount of times, there are two basic ways I can think of, off the top of my head.
#1) Use a while loop, which you seem familiar with already by looking at the code.

count = 0
while count < 3:
  count += 1
  # run all code here

You'll start of by making a variable that keeps track of how many times it has gone through the loop. When the loop starts, it will check if the amount of times it has been run is less than 3. This will stop it from running a fourth time, as the value of count will go 1 after running the first time, 2, and then 3. The next time it tries to run, the test will fail, and the loop will not run. You need to include the line of count += 1 to increase the value of the variable keeping track of how many times it has run.
#2) A for loop. I'll explain how a for loop works in case you haven't used one before.
A for loop has the structure of:
for variable in iterableobject:
An iterableobject is anything that can be iterated over (I'm not going to go in-depth on this explanation). For example, a list of items, such as x = ['a', 'b', 'c']. The variable defined in the for loop changes each time the loop runs. The first time, it will be the first item in the list (or first item yielded in a generator), and the second time, the second, and so on. So to make the loop run three times, you could use a list with three items.

for variable in ['a', 'b', 'c']:
  # run all the code

Or, if you want to keep the code clean, and keep track of which iteration the loop is on, you could make the code like this:

for count in [1, 2, 3]:
  print(count) # will print 1, 2, and then 3
  # run all the code

A clean way to do this, and an easy way, would be to use the python range function:

for count in range(3):
  # run all the code

The range function is a function that generates a list of numbers from 0 to the last number (unless you have two paremeters one for the start, one for the end, but I'll skip over that). So range(3) creates a list that looks like [0, 1, 2]. However, keep in mind that computers start counting from 0, so it begins with 0, and ends right before hitting the last item. So if you're trying to tell the user which iteration they are on, you might want to add 1 when you print it, to make it user-friendly.

for count in range(3):
  print('This is the ' + (count+1) + ' iteration of the loop.') # 1, 2, 3 not 0, 1, 2

Thanks! If you have any questions, please ask!

NathanWeems (5)

@PYer Thank you! I'll try some of those when I get home today.

NathanWeems (5)

@PYer So I still have a problem. When I run it right now, It'll do all of the health at once, and I need help making it not do that

CodeLongAndPros (1624)

Look at line 21 (continue). This makes the loop restart, and is functionally equivalent to while True: pass.
Try commenting it out (prepend with #) and see if that helps.