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
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.
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
for count in range(3): # run all the code
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!