1
class SHM():
    "Class for simple harmonic motion"
    
    # special method to initialise attributes
    def __init__(self, k, m):
        self.k = k    
        self.m = m    
    
    # special method to display it
    def __repr__(self):
        return "{} + {}i".format(self.m,self.k)
    
    # method for angular frequency
    def ang_freq(self):
        return (np.sqrt((self.m)/(self.k))
                
    # method for frequency
    def frequency(self):
        return ((0.5*np.pi)*(np.sqrt((self.m)/(self.k))))
                                       
    # method for period 
    def period(self):
        return ((2*np.pi)/(np.sqrt((self.m)/(self.k))))

I have tried multiple things but I can't understand that why when I run the code my def frequency(self): is saying invalid syntax on the def.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Is that the actual indentation? – Unmitigated Mar 03 '23 at 00:37
  • No in my actual code it is indented. – Erin O'Halloran Mar 03 '23 at 00:38
  • 1
    The problem is caused by mis-matched parentheses on the previous line of code. Please see the linked duplicate to understand why it is reported this way. For reference for future questions, please read the [formatting help](https://stackoverflow.com/help/formatting) to understand how to make the code show up with the correct indentation according to how you actually have it (that is especially important if you want to ask about a syntax error!), and read [mre] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ to understand how to find problems and show **appropriate** code. – Karl Knechtel Mar 03 '23 at 00:38
  • @Unmitigated if that were the problem, the error would have been reported somewhere else. (The code also had a stray backtick from the failed attempt at formatting.) – Karl Knechtel Mar 03 '23 at 00:40
  • 1
    Thank you, it was a silly mistake missing a bracket but it now works. – Erin O'Halloran Mar 03 '23 at 00:45
  • I got it working by adding a parentheses to the end of the `ang_freq` method. I also added `import numpy as np` at the top of the file. – ktm5124 Mar 03 '23 at 01:03
  • One more comment: you can declare the SHM class the way you did, and you can also declare it like this: `class SHM:` (that is, without the parentheses). I personally declare it without parentheses. You can find examples of this syntax on the Python docs: https://docs.python.org/3/tutorial/classes.html#class-definition-syntax. I hope these comments help. – ktm5124 Mar 03 '23 at 01:05

0 Answers0