How do I make it loop
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
Voters
CodeLongAndPros
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.
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.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 go1
after running the first time,2
, and then3
. The next time it tries to run, the test will fail, and the loop will not run. You need to include the line ofcount += 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']
. Thevariable
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.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:
A clean way to do this, and an easy way, would be to use the python
range
function: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). Sorange(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.Thanks! If you have any questions, please ask!
@PYer Thank you! I'll try some of those when I get home today.
@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