Exercise 2.1-Can't figure out what I'm doing

I’m in the Python 3 for Robotics area at the 1st exercise-2.1. I keep getting syntax errors. What am I doing wrong, is this the way it’s supposed to be?
If I’m doing it wrong, how do I straighten it out and get what I’m supposed to get? Below are some copies of what I was doing, the 1st two are from the beginning of the exercise, and the last one is from the bottom of my exercise.
Note: you might have to enlarge them to see them.

What are the descriptions of the syntax errors you are getting? Usually, the error messages help you find out what is wrong with your code.

I would recommend checking the following:

  • Make sure robot_control_class.py is precisely the same as the guide from the notebook.

  • Include parenthesis when you define the variable that instantiates the RobotControl class.
    rc = RobotControl()

  • To get the value from the get_laser() method, we need to call it from an instance of the RobotControl class (this is why we defined rc above). To call a method, we write the name of the instance (rc) followed by a dot ‘.’ and the method name. We can assign this value to a variable as follows:
    value = rc.get_laser(360)
    (Note that you can set the variable name as a, value, val, etc.)

  • When you want to print multiple elements, you separate them using commas. Remember that strings must be inside double quotes or single quotes. If we want to output a message (a string), a variable, and another message (another string) in a single line, a valid format would be:
    print(‘First part of the message’, variable_name, ‘second part of the message’)

Classes, methods, and instances will be covered in detail later in the course.

I hope this helps.
-Allan

Hi @allens,

as @acotto already pointed out please post the error message that you get. Don’t worry about having issues, we are here to help you get them fixed and you will learn a lot during the process. But please there are some minimal pieces of information that we need to be able to help. These are:

  • The course, unit, and exercise number (you already provided that).
  • The error message
  • The relevant piece of code where the error is happening, this is normally indicated by the error message as a line number

This information will help us start the debugging, afterwards we might ask for additional tests or code snippets, but the above is the minimum that we need to help you in situations like this.

Looking forwards to your update,

Cheers,

Roberto

Hi @allens,

based on the screenshot that you sent, the problem is that you have to paste the python code inside the robot_control_class.py file using the Code Editor, but you have pasted the content in the terminal instead of the Code Editor.

Please open the right file using the Code Editor, and then paste the content there.

Please let us know how it goes.

can you show me what the Code Editor is and how to paste code into files using the Code Editor?

Hi @allens,

Code Editor is the same IDE (Integrated Development Environment) that is explained in the following post:

So what you’re saying is, don’t copy, paste & execute the first five files (“cd ~/catkin_ws/src/” - through - “touch robot_control_class.py”) into shell #1 but instead, use the IDE method to set up the program correctly.
If this is the case, It would have been a lot better for everybody if you would have just told people to do that in the first place.
However, going by this


it seems to say I can use both methods to set up the program correctly. This is what I was going by.

Hi @allens,

We use the touch command to create new empty file(s) from the command line. Alternatively, if you prefer to use the IDE (the code editor) you can right click on a folder and create a new empty file by selecting the “New File” option in the context menu.

For adding code to an empty file we use the IDE (code editor).

So after the file creation, you should see the new filenames inside in the IDE’s left sidebar file browser.

To add code to a new file you have to first open a file.

To open a file in your IDE you just have to click the file name in the IDE’s left sidebar file browser using your mouse. When you click on a file it opens in a new tab inside the IDE main area. Notice that the tab is displaying the filename. Observe that each time you click a different file name it will open inside the IDE main area.

To copy code from the lectures you can click on the ‘copy code to clipboard’ button at the top of each code block (or alternatively select the code you want and press Ctrl+C to copy.)

To paste code open the file where you want to add the code using the IDE. Make sure you can write text: the cursor should be blinking off and on, and then press Ctrl+V to paste.

I have now added detailed instructions on how to add code from the lectures to files to help future students.

Cheers,

Roberto

1 Like

I’ve got everything set up correctly, I believe, but how do I Execute in the IDE. I couldn’t find how to Execute the IDE so I tried to Execute by doing the (copy from the files and paste method into shell #1). Below is what It looks like when I try to do the copy and paste method.

Hello @allens,

we use the python or python3 command for executing a Python script. This command must be used along with the file path to your script. Most of the time, we tend to navigate to the file’s directory and run the command with just the file name instead of providing the full file path.

If you follow along the commands as instructed by the course you will be already located inside the directory where the pyscript1.py file is located. Have a look at the screenshot below:

If, for some reason, you are not located inside the directory where you created that file, then use the change directory command cd to navigate where the file is located.

In this case it would be:

cd ~/catkin_ws/src/robot_control

Please confirm that you are inside that directory, then you can execute:’

python pyscript1.py

Hope this helps,

Roberto

Roberto, thanks for getting back with me I’m going to try the method that you suggested but 1st, I want to be able to execute the program in the (IDE). How do I execute the program in the IDE?

Hello @allens,

I am sorry I don’t understand what do you mean by “execute the program in the (IDE)”? Could you please clarify?

In my case, and I think this is the general case, I use the IDE (code editor) to create files and write code. And then I use the command line/terminal or webshell (in this case all synonyms) to execute or run those programs created using the IDE. This is why at the moment I don’t understand your question or what you are trying to do or achieve.

Cheers,

Roberto

1 Like

Things are starting to get out of hand, so lets try this another way.
Lets start from the beginning.
I’m going to copy & paste the command lines into shell #1. When I run into a problem, I’m going to stop and highlight it. You then with as little words as possible, show me how to fix it.
1st problem: Copy-pasting and executing the MKDIR into the shell #1.

Hi @allens ,

I have a suggestion that may be a fix for you.

I see that you have just started working on this course, so I believe that you would not have a lot of files saved.

Try the following steps in the exact order if your ~/catkin_ws/src/ folder is empty.

cd ~/catkin_ws
rm -rf ./build ./devel ./src
mkdir src
catkin_make
cd src
mkdir robot_control

If your ~/catkin_ws/src/ folder is not empty, then download the contents of the src folder by right clicking on src and clicking “Download”. It will get downloaded to your computer as a compressed file.
Now do the above steps indicated as a command code-block.
Create the respective files inside src folder and copy-paste the contents from the downloaded compressed file.

This should fix your problem. Let me/us know if you still have problems.

Regards,
Girish

Hello @allens,

let me explain this error message to you:

mkdir: cannot create directory 'robot_control': File exists

This error message is informing you that the “mkdir” command was unable to create a directory named “robot_control” because a directory with that name already exists in the current location.

The “mkdir” command is used in Linux to create a new directory (folder). However, it can only create a new directory if there is no directory with the same name in the same location.

In this case, it appears that you executed that command previously in the course. So there is no need to create that directory again.

I strongly recommend that you consider to start by completing the “Linux for Robotics” course first. This course will teach you how to use the command line and the most commonly used commands, including “mkdir”.

Cheers,

Roberto

Lets stick to the immediate problem at hand. How do I fix the mkdir problem in this program?

Hi @allens since you already have created that directory you can skip that step, there is no need for you to run the

mkdir robot_control

command again.

Cheers,

Roberto

Ok great, so how do I move forward in this program without using the mkdir command?

Python for Robotics
Python 3
Unit 2, Python Essentials
Exercise 2.1

This is what happens when I continue using the rest of the commands and the (#!/usr/bin/env python) command without using the mkdir command. How do I fix this problem?
NOTE: The Command Not Found & The Syntax errors


Hi @allens ,

After clicking the “copy” button, you must paste the copied contents into the robot_control_class.py file and not on the terminal itself. This is one of the mistakes that you are doing.

Once you have pasted the contents into the python file, you must make it an executable.
To do that, you must navigate to the the folder that contains that python file using the terminal.
Now you do chmod +x <name_of_your_python_file>.py, which in your case would be:
chmod +x robot_control_class.py.
Also do the same for pyscript1.py.

You can then proceed with the later steps in the tutorial.

Hope this helped.

Regards,
Girish