How to Add Text in CodeHS

Posted on

Now, you may be learning coding in CodeHS and now you are struggling to add text in CodeHS. Actually, if you want to know how to add text in CodeHS, you are able to watch a video on CodeHS Youtube channel where the title is Tracy – Adding Text which was uploaded on August 21st, 2020. So, here is the explanation about adding text in CodeHS according to the Tracy – Adding Text video uploaded by CodeHS.

There is the Write function where it is a new command that permits us to add labels to the screen. You can simply insert the text that you want to write as a parameter in the function. You are able to print strings or integers to your screen this way.

Now, on the video, you can see Using turtle.write where there is write (“Hello World”) in the number one. It is explained that if we call this function with the text Hello World, we will see that label appear at Tracy’s location on the screen. It looks very small. Now, let’s see a way how we can change the size. In the Adding the font Attribute, you are able to change the size and style of the font used to create the label. You have to include this information inside a set of parentheses where each item is separated by a comma.

write(text, font = (type, size))

The type of font or font name comes first. This name is written inside quotation marks. Afew examples that you are able to use are Ariel, Times New Roman, and Futura. The size of the font comes second and it is written as an integer value that notes the size of the font in pixels. When you add the font attribute with the font name of Futura and a size of 20, you can see that the font is bigger and has changed in style.

The third attribute that you can use when you use the Write Function is a line. It will determine where the text will be drawn in relation to Tracy’s position. The options we are able to use here are center left or right, all written inside quotation marks. An important thing to note is that the attributes of font and align are optional and can be called in any order inside the Write Function. Even though you have to add the text to print as the first parameter, you are able to choose to add whichever additional parameters you want and can place them in either order.

When you add the align attribute to the Write Function and set the location to center, you are able to see that the text is centered above Tracy’s location. You are able to use variables to print a label the same way you use variables in other parts of the programs. You are able to just call the variable name where the text should be included in the write function and the value saved in that variable will be printed.

Let’s say that you want to add strings together, say in this case, where you want to create a greeting depending on the name saved inside a variable, we use something called concatenation. To concatenate strings, you place a plus sign between the different items and they will all be pieced together to form the final string in this case Hello Tracy.

1 name = “Tracy”

2 write(“Hello ” + name, font = (“Futura”, 30), align = “center”)

You have to note that you have to include a space after the string Hello. If you do not add a space, there will be no space between the string Hello and the string Tracy. You also have to remember that if you want to use the value of the variable, you do not include quotation marks around the variable name.

Then, what do we have to do if we want to concatenate a string and a number. If you try to do this the same way you concatenated strings, you will get an error that tells you that you cannot concatenate str and int objects. Then, in the video, she explains that think back to when we tried to perform mathematical functions on values input by a user. We needed to use the int keyword around the variable to change its value to an integer type. So, you are able to do the same thing here to be able to fix this error.

If we surround the variable name that has an integer value with the keyword str, it will change the type of the variable to a string which can then be concatenated with another string and you will see the output that you are expecting.

In the video, you are able to take a look at an example in the editor. In the example, we want to add labels to the concentric circles program. We wrote previously that note the radius values of each circle drawn. Now, you can see how this can be done.

In the video, you are able to see that there is the concentric circles program. When we run it, it asks the user for input and then draws three circles based on the sizes that the user entered. Now, the thing that we want to do is to add a label beneath each circle to say what the radius of each circle is.

So, inside the draw circle function, after the circle is drawn, she wants to have Tracy write the radius value. She can just use this radius number that is set in as her argument to her function to write that number, as you can see in the video. Let’s say that we want it to be above where Tracy is. So, we will align it at the center and then you can see in the video what happens if we add this one line of code. Now, the Tracy is writing the number at the bottom of each circle. So, that’s a good start.

Well, these steps do not end yet. There are still more steps and you can watch the complete steps by watching the video on Youtube in CodeHS channel and the title of the video is Tracy-Adding Text.

Leave a Reply

Your email address will not be published.