Classes and Objects in Python

Python Land Forums The Python Tutorial Classes and Objects in Python

Viewing 6 reply threads
  • Author
    Posts
    • #1890
      guillaume
      Participant

      Good day,

      Thank you very much for this tutorial I am really enjoying it. I am still very much a noob so any help would be appriciated.

      If i try to do the exsample under Creating classes and objects in Python

      I get the error SyntaxError: invalid syntax. Even if I copy and pates your’s into my VSC

      Any help would be appriciated

      Kind regards

    • #1891
      Erik van Baaren
      Keymaster

      Could you copy and paste the exact error for me?

    • #1892
      guillaume
      Participant

      Hi Erik,

      Looks like I got it working. I restarted my computer for VSO to work!

      Thank you very much!

    • #1893
      guillaume
      Participant

      Hi Erik,

      Getting the error again

      >>> car.increase_speed(10)
      Traceback (most recent call last):
      File “<stdin>”, line 1, in <module>
      NameError: name ‘car’ is not defined

      class Car:
          speed = 0
          started = False
       
          def start(self):
              self.started = True
              print("Car started, let's ride!")
       
          def increase_speed(self, delta):
              if self.started:
                  self.speed = self.speed + delta
                  print('Vrooooom!')
              else:
                  print("You need to start the car first")
       
          def stop(self):
              self.speed = 0
              print('Halting')
      car = Car()
      • This reply was modified 1 week, 1 day ago by Erik van Baaren. Reason: fixed the markup
      • #1894
        Erik van Baaren
        Keymaster

        I tried it myself to see if I can replicate your problem. What might be happening, is that you copied the class and didn’t hit enter twice after that. If you enter a multi-line statement, function, or class, you need to hit enter twice to finish it.

        Hope that helps!

      • #1897
        Jack
        Moderator

        Yes I think that’s exactly what’s happening. I see the page got updated to explicitly tell you to hit enter twice after pasting the class. Only after that, you can create a car object.

        So first copy-paste this:

        
        class Car:
            speed = 0
            started = False
         
            def start(self):
                self.started = True
                print("Car started, let's ride!")
         
            def increase_speed(self, delta):
                if self.started:
                    self.speed = self.speed + delta
                    print('Vrooooom!')
                else:
                    print("You need to start the car first")
         
            def stop(self):
                self.speed = 0
                print('Halting')
        car = Car()
        

        And hit enter twice. Then you can use the class, e.g. with car = Car()

        • This reply was modified 1 week, 1 day ago by Jack.
        • This reply was modified 1 week, 1 day ago by Jack.
    • #1902
      guillaume
      Participant

      Hey Guys,

      Really appreciate the feedback and help.
      I really cant figure out what I am doing wrong I have tried to copy and pasting the whole thing and I have tried to type it line by line followed by hitting enter twice  after the car = Car() still getting this error

      Traceback (most recent call last):
      File “<stdin>”, line 1, in <module>
      NameError: name ‘car’ is not defined

      When entering car.increase_speed(10)

      • #1911
        Erik van Baaren
        Keymaster

        Let me put this another way. In your code example, you let the definition of car directly follow the class definition. I mean the line car = Car().

        If you copy-paste or enter it like that in the REPL, it won’t work. You need to keep hitting enter after the class definition (effectively two times) until you get a new prompt (the >>>).  So if I do this on my pc, it looks like this:

        Python 3.8.5 (default, Jul 28 2020, 12:59:40)
        [GCC 9.3.0] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> class Car:
        ...     speed = 0
        ...     started = False
        ...
        ...     def start(self):
        ...         self.started = True
        ...         print("Car started, let's ride!")
        ...
        ...     def increase_speed(self, delta):
        ...         if self.started:
        ...             self.speed = self.speed + delta
        ...             print('Vrooooom!')
        ...         else:
        ...             print("You need to start the car first")
        ...
        ...     def stop(self):
        ...         self.speed = 0
        ...         print('Halting')
        ...
        >>> car = Car()
        >>> car.increase_speed(10)
        You need to start the car first
        >>>
        

        As mentioned below, I’d suggest staying away from files for now and try to get this working in the REPL first.

        Hope that clears it up!

    • #1903
      guillaume
      Participant

      Here is a link to the screen shot I took (https://we.tl/t-18APy5U24O)

      • #1914
        Erik van Baaren
        Keymaster

        I see what happened, the forum holds any post with a link for moderation (because of spammers, unfortunately). Looking at your screenshot, I can see two problems:

        * You’re mixing code in a file with the REPL
        * It looks like you didn’t save the file (control + s)

        Not sure what the reason is here, but I’d suggest staying in the REPL and getting it working there first before you start creating and using files.

    • #1929
      guillaume
      Participant

      Hi Erik,

      I manadge to figer out the problem was with VSO if I coppied it over to powershell/pythom3 it worked as you instructed.

       

      Thank you for all the help

Viewing 6 reply threads
  • You must be logged in to reply to this topic.