Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:30:02+00:00 2026-05-23T02:30:02+00:00

I was wondering how to fix this problem I am having with my first

  • 0

I was wondering how to fix this problem I am having with my first piece of OOP code. The problem lies within the attack method of the Snake class. I have two snakes in the game and am trying to get the snake to attack the other one. At the moment I am using two variables to note which Snake’s turn it is, then use this to try and attack the other snake. However this is not working. Anyone have any idea how to solve this? Thank you so much.

class Snake:
    hp=100
    attack=25
    defense=1
    def set_name(self, name):
        self.name=name

    def shed(self):
        self.defense=self.defense+1

    def attack(self, opposite, current):
        opposite.hp=opposite.hp-(current.attack-opposite.defense)

    def eat(self):
        self.attack=self.attack+5
        print(str(self.name) + " eats a rat!")
        print(str(self.name) + "'s attack dmg is now " + str(self.attack))

    def sleep(self):
        print (str(self.name) + " goes to sleep")
        self.hp=self.hp+10
        if self.hp>100:
            self.hp=100
        print (str(self.name) + " wakes up with " + str(self.hp) + "hp")

##initialises the snakes
alpha=Snake()
beta=Snake()
## gives the snakes names of the user's choice
alpha_name=raw_input("What would you like to name your snake? ")
alpha.set_name(alpha_name)
beta_name=raw_input("What would you like to name the other snake? ")
beta.set_name(beta_name)
##starts the game
turn=True
while alpha.hp>0 and beta.hp>0:
    while turn==True:
        opposite="beta"
        current="alpha"
        action=raw_input("attack, sleep, eat or shed? ")
        try:
            if action=="attack":
                alpha.attack(opposite, current)
            if action=="sleep":
                alpha.sleep()
            if action=="eat":
                alpha.eat()
            if action=="shed":
                alpha.shed()

            turn=False
        except IOError:
            print("Please chose only one action, exaclty how it is typed")
    while turn==False:
        opposite="alpha"
        current="beta"
        if beta.hp<15:
            beta.sleep()
        elif alpha.hp>75:
            beta.attack()
        else:
            index=random.randint(1, 3)
            if index==1:
                beta.shed()
            elif index==2:
                beta.eat()
            else:
                beta.attack(opposite, current)    
        turn=True
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:30:03+00:00Added an answer on May 23, 2026 at 2:30 am

    in “attack” you try to access “opposite.hp”, but this method is called with a string instead of an object:

    opposite="alpha"
    current="beta"
    

    => change this to

    opposite=alpha
    current=beta
    

    also, there is a field and a method with the same name in the class: attack. I suggest renaming the field to “attackpoints” or something.

    additionaly, you call “beta.attack()”. you forgot the method arguments there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that snapshot isolation would fix this problem, but I'm wondering if NOLOCK
I'm wondering if this is something somewhat simple, but I'm having a problem ONLY
I'm wondering how to fix this problem in Zepto library. It happend's when I
I was wondering if anyone else has figured out how to solve this problem.
Having a strange problem with CJ's Product Catalog Search Service . Wondering if anyone
I can't seem to figure out this problem for some reason my piece of
im having a little problem with the Google API maps, i was wondering if
I was wondering how one can fix an upper limit for the length of
I want test a FIX gateway for our company and was wondering if anything
Wondering if anyone can help me with this annoying but trivial (in terms of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.