Question

## python oop creating a large number of instances ## im trying to create a large...

## python oop creating a large number of instances

## im trying to create a large number of instances for a class. could you show me a way to make it work
import math
import random
class soldiers:

def __init__(self,x,y):
self.x = x
self.y = y

def get_target(self,x,y,number_red_soldiers,number_blue_soldiers):
pass
# self.target =

def aiming_angle(self,x,y,target,enemy_x,enemy_y):
dy = self.y-enemy_y[target]
dx = self.x-enemy_x[target]
angle =math.atan(dy/dx)

a = 0
number_blue_soldiers= 10
number_red_soldiers = 10
for i in range(number_blue_soldiers):
blue_x = random.randint(0,100,10)
blue_y = random.randint(0,100,10)
for i in range(number_red_soldiers):
red_x = random.randint(0,100,10)
red_y = random.randint(0,100,10)

for i in range(number_of_blue_soldiers): ### here im trying to create a lot of instances
bluesoldier+str(i)= soldiers(blue_x[i],blue_y[i]) # i want to instance 10 soldiers called bluesoldier1 bluesoldier bluesoldier3 bluesoldier4 etc.....
while a < 99999:
print(bluesoldier1.x) ### and print soldier 1's x position

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import math
import random
class soldiers:

        def __init__(self,x,y):
                self.x = x
                self.y = y

        def get_target(self,x,y,number_red_soldiers,number_blue_soldiers):
                pass
                # self.target =

        def aiming_angle(self,x,y,target,enemy_x,enemy_y):
                dy = self.y-enemy_y[target]
                dx = self.x-enemy_x[target]
                angle =math.atan(dy/dx)

a = 0
number_blue_soldiers= 10
number_red_soldiers = 10

blue_x = []
blue_y = []
red_x = []
red_y = []
for i in range(number_blue_soldiers):
        blue_x.append(random.randint(0,100))
        blue_y.append(random.randint(0,100))

for i in range(number_red_soldiers):
        red_x.append(random.randint(0,100))
        red_y.append(random.randint(0,100))

# You can not create the variables as bluesoldier1, bluesoldier2 dynamically,
# rather you need to put them in a list
blueSoldiers = []
redSoldiers = []
for i in range(number_blue_soldiers): 
        newBlueSoldier = soldiers(blue_x[i],blue_y[i])
        blueSoldiers.append(newBlueSoldier)

for i in range(number_red_soldiers): 
        newRedSoldier = soldiers(red_x[i],red_y[i])
        redSoldiers.append(newRedSoldier)

# Now print blue soldiers position
for b in blueSoldiers:
        print(b.x, b.y)

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
## python oop creating a large number of instances ## im trying to create a large...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #python oop referencing attributes in objects # problem is the angle function in my class. import...

    #python oop referencing attributes in objects # problem is the angle function in my class. import random class people(): def __init__(self,x,y,size_of_other_team): self.x=x self.y=y self.target = random.randint(0,size_of_other_team) # choose a random guy from other team def angle(self, other_teams_x, other_teams_y): # get angle between this guy and his target dx = other_teams_x[self.target]-self.x dy = other_teams_y[self.target]-self.y theta = math.atan(dy/dx) return(theta) def move(self): self.x+=1 red_guys = 10 blue_guys = 10 red_x=[] red_y=[] red=[] # objects list for i in range(red_guys): red_x.append(random.randint(0,100)) red_y.append(random.randint(0,100)) red_person =...

  • [Problem 5] List all that the following Python class is missing in order to function. [10...

    [Problem 5] List all that the following Python class is missing in order to function. [10 Points] import math class TwoCoordinatePoints (object): #This class represents a point A = (x, y) with integer values for x and y. -_init__(x, y): self.x = x self.y = y def -_str__(self): "(%, %)" % (self.x, self.y) def distance (self, P): #computes the distance between two points (instances) of this class return math.sqrt((self.x-P.x)**2+(self.y-P.y)**2)

  • Please convert the following python program to LINE BY LINE python pseudocode. Please convert LINE BY...

    Please convert the following python program to LINE BY LINE python pseudocode. Please convert LINE BY LINE (typed) class vector: def __init__(self,x,y): self.x = x # Assigning x to vector's x self.y = y # Assigning y to vector's y def __str__(self): return '('+str(self.x)+","+str(self.y)+')' # This returns the value of vector in bracket format def __add__(self,other): x = self.x+other.x # values of x of vector1 and vector2 are added forming new x value y = self.y+other.y # same is done...

  • Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):...

    Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):         self.x = x         self.y = y         self.width = width         self.height = height     def show(self):         turtle.up()         turtle.goto(x, y)         turtle.down()         for _ in range(2):             turtle.forward(width)             turtle.right(90)             turtle.forward(height)             turtle.right(90) bigbox = Box(-100, -150, 200, 300) bigbox.show() turtle.done() If you run the above program, it will produce an error. What is the keyword that has...

  • Python question. How do i make this code work? im trying to print the value of...

    Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value)    def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • 10p Python For the following question, refer to the Python module on the right, as well...

    10p Python For the following question, refer to the Python module on the right, as well as the code below. 1) What is the output of the following code? #dodgeable.py #main dodgeable.py import dodgeable class Locatable: def NUM_CARS = 3 init (self,x,y): NUM TRUCKS = 2 self.mX = X WIDTH = 60 self.mY = y def setx (self,x): def main (): self.mX = x object_list = [] def sety (self, y): self.mY = y for i in range (NUM_CARS) :...

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • Score Analysis creating a class named "Student" in python and use matplotlib package

    class students:       count=0       def __init__(self, name):           self.name = name           self.scores = []           students.count = students.count + 1       def enterScore(self):                     for i in range(4):                 m = int(input("Enter the marks of %s in %d subject: "%(self.name, i+1)))                self.scores.append(m)             def average(self):                 avg=sum(self.scores)/len(self.scores)                           return avg               def highestScore(self):                           return max(self.scores)         def display(self):                          print (self.name, "got ", self.scores)                      print("Average score is ",average(self))                          print("Highest Score among these courses is",higestScore(self))          name = input("Enter the name of Student:")  s = students(name)  s.enterScore()  s.average()  s.highestScore()  s.display()So I need to print two things, first one is to compute the average score of these courses and second one is to print out the course name with the highest score among these courses. Moreover I need to import matplotlib.pyplot as plt to show the score figure. I barely could pull the code this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT