#๐Ÿ”’ Help with loops

60 messages ยท Page 1 of 1 (latest)

bronze crypt
#

I've been building little battle system and the logic "works," but I cant get the loop to go through again. Any help is appreciated.

#battle script
import random
rng = random.randint(13,20)

class Player:

    def __init__(self,name):
        self.name = name
        self.hp = 25
        self.atk = 5
        self.defs = 3


class Enemy:
    def __init__(self,name):
        self.name = name
        self.hp = 25
        self.atk = 5
        self.defs = 3

class Attack:
    def __init__(self):
        player = Player("Zyx")
        enemy = Enemy("Orc")

        while player.hp or enemy.hp > 0:
            swingCheck = rng
            #print(swingCheck)
            if swingCheck >= 13:
                playerHp = player.hp
                #print(player.hp)
                enemyHP = enemy.hp
                atkVsDef = player.atk - enemy.defs
                currentEnemyHP = enemy.hp - atkVsDef
                print(currentEnemyHP)
            
attack = Attack()
narrow nimbusBOT
#

Hey @bronze crypt!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

@bronze crypt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

limber forum
#

As well as that, this won't work as you expect. You're never using the player and enemy object you create outside of the __init__.

bronze crypt
#

Can you show how it should be done?

limber forum
#

Well, what I mentioned is separate from the main question.

ebon ridge
#

yeah i think you have a misunderstanding of what OOP is and what its used for

limber forum
#

For what I mentioned, note that every time you do Player("Zyx"), you're creating a new player object separate from any others. You do Player("Zyx") twice, so you create one object outside of the class, then another inside. You probably intend to give __init__ parameters and pass those objects in.

ebon ridge
#

you are essentially doing this:

a = [1, 2, 3]
b = [1, 2, 3]

and expecting modifications of a to effect b

bronze crypt
bronze crypt
limber forum
#

Ah. And for the loop problem, what behavior are you expecting?

#

Also note, you only generate rng once, so that will always be the same value. You need to call randint to generate a new number.

bronze crypt
#

I want the loop to continue if the health has not dropped to 0

bronze crypt
limber forum
#

Oh, your loop condition is wrong.

bronze crypt
#

How should I do the condition?

limber forum
#

Actually, it's technically wrong but still does what you want.

bronze crypt
#

lol, except it only does the loop once

limber forum
#

You need the > check for both health values, in case you were expecting the > check to apply to both health values.

#

!or

narrow nimbusBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
limber forum
#

Actually that would be a problem since negative values will cause it to loop forever.

#

So ya, fix that.

bronze crypt
#

while player.hp > 0 or enemy.hp > 0:?

limber forum
#

Ya. I don't see why it would only go once though, since now that I read the code again, you are also never changing their health values in the loop. You just create new variables like enemyHP, but never change enemy.hp.

bronze crypt
#

enemy.hp = currentEnemyHp

limber forum
#

Ya, like that.

bronze crypt
#

The loop keeps going, but it doesn't keep triggering the conditional

limber forum
#

Show your new code.

bronze crypt
#
class Attack:

    def __init__(self):
        player = Player("Zyx")
        enemy = Enemy("Orc")

        while player.hp > 0 or enemy.hp > 0:
            swingCheck = rng
            #print(swingCheck)
            if swingCheck >= 13:
                playerHp = player.hp
                #print(player.hp)
                enemyHP = enemy.hp
                atkVsDef = player.atk - enemy.defs
                currentEnemyHP = enemy.hp - atkVsDef
                enemy.hp = currentEnemyHP
                print(currentEnemyHP)
            
attack = Attack()
narrow nimbusBOT
#

Hey @bronze crypt!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
bronze crypt
#

it works, but now it goes into the negatives, lol

limber forum
#

The loop condition also needs to use and.

#

The current condition will loop while the player or the enemy are alive.

#

And the player is never damaged, so the loop will go forever.

bronze crypt
#

It stops at -1 from 1, so that is 1-2 = -1

#

It works! Thank you!

limber forum
#

You're welcome. I would recommend writing and testing small pieces of code at a time. You had a couple of issues there, and trying to debug multiple problems at once complicates things.

bronze crypt
#

You are totally right, but I needed the enemy and player class to test this properly

#

Do you mind asnwering a couple questions about this code at scale?

limber forum
#

Sure

bronze crypt
#

Ok, I'll send a git link, so you can see the whole project

limber forum
#
if playerInput == 'attack' or "Attack":
#

Another or gotcha

bronze crypt
#

Gotcha, always use and when trying to check multiple conditions when either or are to be true

limber forum
#

No, there, you need to compare playerInput against both strings separately:

#
if playerInput == 'attack' or playerInput == "Attack":
#

or

if playerInput.lower() == 'attack':
#

I have to get off soon though, so, did you have a question?

bronze crypt
#

I'm trying to make this battle system modular, so it works for both the enemy attacks and player attacks

#

Is there a a better way to handle this? It feels like my system is a bit rudimentary and will require duplication of the code for the enemies to attack the player

limber forum
#

Sorry, I thought it'd be a easier question lmao. I don't have enough time to understand the code enough to give suggestions in my current state.

#

Have a good night.

bronze crypt
#

Thank you for the help you provided. I appreciate you. Have a good night

karmic agate
#

if your post was automatically closed when you come back and if you still need help feel free to open a new topic and ping me.

narrow nimbusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.