'AttributeError: 'str' object has no attribute 'player_health' [closed]

today I decided to learn OOP in python and I'm currently playing around with it to understand it a bit, I am trying to make a terminal based rpg or battle game of some sort and ran into the

"AttributeError: 'str' object has no attribute 'player_health'" problem

I only ran into it when I change the "or" to "and" in the while statement, I don't really get why this wouldn't work for "and"?? Cause I'm trying to make it so that when one of the healths go below zero, the loop stops? can you help me? Thank you so much!

import random

class Enemy():
    def __init__(self, species, enemy_health):
        self.species = species
        self.enemy_health = enemy_health
    def attack(self):
        print("has attacked you!")
    def heal(self, health):
        health =++ 10
        
    def lose_enemy_health(self):
        self.enemy_health -= random.randint(0, 10)
        
class Player():
    def __init__(self, player, player_health):
        self.player = player
        self.player_health = player_health
        
    def attack(self):
        print("You attacked!!")
        
    def heal(self, player_health):
        health =++ 10
        
    def lose_player_health(self, player_health):
        self.player_health =- random.randint(0, 1)
        
enemies = ["Vampire", "Zombie"]
enemy_fight = random.choice(enemies)
enemy = Enemy(enemy_fight, 100)
player = input("Enter your player name: ")
p = Player(player, 150)
print("Welcome",player,"to text based RPG!!")

while enemy.enemy_health and player.player_health > 0:
    player_attack = input("Attack or heal?: ").lower()
    if player_attack == "attack":
        #print(enemy.enemy_health)
        enemy.lose_enemy_health()
        print("The enemy now only has",enemy.enemy_health,"health!")
    else:
        break


Solution 1:[1]

At first, the error only occurs with and, because it doesn't get called with or, because the first condition is true. And the variable player is a string and not an instance of the class Player.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 konstantin durant