unsupported operand for 3 instances of two classes and one method?

2024/10/6 12:31:22

I'm trying to get the program to take the hp stat from enemyUnit, the attack stat from unit, and the damage stat from tackle and put them into one math problem in the method getHit(). this is the code:

class Pokemon(object):    def __init__(self,hp,attack,defence):self.hp = hpself.attack = attackself.defence = defencedef getHit(self,damage,hp,attack):self.hp -= damage * self.attack/self.defenceprint str(self.hp)class Move(object):def __init__(self,damage):self.damage = damageunit = Pokemon(10,2,3)
enemyUnit = Pokemon(4,2,3)
tackle = Move(3)enemyUnit.getHit(enemyUnit,tackle,unit)

unfortunately it gives me the error

unsupported operand type(s) for *: 'Pokemon' and 'int'

how do I get it to take all the variables from all instances of each class and put them into one function?

Answer

In the getHit() method you are passing the arguments:

  • hp
  • attack
  • defence

but when you call it in enemyUnit.getHit(enemyUnit, tackle, unit) you are passing enemyUnit which is an object of the Pokemon class. This causes the error.

You may want to pass the correct parameters in:

enemyUnit.getHit(...) # Correct the parameters

Edit:

I think the best idea would be to implement the getHit() method like this:

def getHit(self, other, move): self.hp -= move.damage * other.attack / self.defenceprint str(self.hp)

and call it with:

enemyUnit.getHit(unit, tackle)

Note that you were passing hp and attack, which are attributes of Pokemon. You can use them by just calling obj.hp and obj.attack.

https://en.xdnf.cn/q/118954.html

Related Q&A

Telling the script to wait until button is clickable? [duplicate]

This question already has answers here:Check whether element is clickable in selenium(3 answers)Closed 4 years ago.Im writing a script in Selenium (Python) and I am having an issue with a disabled clic…

Python: UnicodeDecodeError: utf8

Im having problem to save accented letters. Im using POSTGRESQL and Python 2.7POSTGRESQL - ENCODING = LATIN1I already added this line but does not worked! #!/usr/bin/python # -*- coding: UTF-8 -*-More…

Get continuous response of POST request in python

Im writing a script which does a POST request to a server and gets blocked the server keeps sending the response whenever a specific event is triggered. I have to take a cookie for post request with ea…

DataType of Pandas Multiindex [duplicate]

This question already has answers here:get the dtype of a pandas multiindex(3 answers)Closed 6 years ago.import pandas as pd index = pd.MultiIndex.from_tuples([(1,2, None), (1,2,3)]) print(index.get_le…

Is there a way to see the internal representation of float?

In python tutorial for floating point, In base 2, 1/10 is the infinitely repeating fraction0.0001100110011001100110011001100110011001100110011...How do I get python interpreter to print this intenal re…

Create function from try-except

Based on the example provided in this answer, how can I create a function from:from collections import Counter s = [0, 0, 2, 1, 1, 0, 0, 0] try:print(next(t[0] for t in Counter(s).most_common(2) if t[…

Bad request error flask with POST request

I have the following route that is getting values from a submitted form@app.route(/authenticate, methods=["POST"]) def authenticate():username = request.form[username]print(username, file = s…

How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

Im wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity…

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…