Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Class for Vector
#1
Hello! Angel
I am new in programming in python. I got a homework to create class MyVector,
- constructor will accept (to self) one parameter (it will be one-dimensional array)
- method get_vector(self) return one-dimensional array containing the vector elements
- with special method __mul__(self,other) implement the dot product of two objects MyVector (the output is a scalar, so one number)
btw I cant import other modules.

I have this:
İmage


In my mind, get_vector(self) is right. But __mul__(self,other) is not working properly. Do you know how to make things right? Blush
Quote
#2
Please, don't post images of code. Copy/paste in python tags.
See BBcode help for more info.
Quote
#3
(Oct-06-2019, 02:44 PM)buran Wrote: Please, don't post images of code. Copy/paste in python tags. See BBcode help for more info.
Sorry, there is:
class MyVector:
  def __init__(self, a):
    self.a = a

  def get_vector(self):
    return(self.a)

  def __mul__(self, other):
    dot_product = 0
    for x in range(len(self.a)):
      dot_product += self.a[x]*other.a[x]
      return dot_product
Quote
#4
You need to dedent line 12 - i.e. you want to return dot_product only after the for loop ended.

I understand that this is homework and you need to do what you are asked to do. However, for your information, using get function like this get_vector is not pythonic. i.e. you can just access the a property (of cource you want more meaningful name)
Quote
#5
Oh, thanks. I didnt notice!
As you said - we need to use this "unpythonic" method, but thank you for info.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Divide a vector Langosmon 1 720 May-13-2018, 09:09 AM
Last Post: ThiefOfTime
  vector sum of tuples roadrage 7 2,721 Nov-28-2016, 09:54 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)