huw-rhys-jones / Project-Vulcan

Attempting to read manuscripts from Herculaneum
GNU General Public License v3.0
1 stars 0 forks source link

Preparation 4: More on Types #7

Open huw-rhys-jones opened 1 year ago

huw-rhys-jones commented 1 year ago

In this assignment, we are going to learn some additional things about types in Python.

Let's fire up our Codespace from Issue #6. If you go to your branch, click <> Code and Codespaces, it should be there. It'll have some weird randomly generated name.

image

I have created two new files for this homework which are both on the original Homework branch. We need to 'checkout' these files.

The first thing you need to do is open up a terminal. Click the three dashes in the top left corner, go down to Terminal then select New Terminal. It should open a new terminal at the bottom of the screen.

We need to checkout the Homework branch to get the new files I have added:

git checkout Homework

You are now on the Homework branch and should be able to see two new files in the tree (task1.py and task2.py). We need to switch back to your branch and bring these files over. Enter the following into the terminal:

git checkout <your-branch-name>

replace with the name of your branch! Next, we want to bring those files over:

git checkout Homework task1.py task2.py

image

These files should now be visible in the file tree on the left. Open them up in turn, read through the instructions and attempt to complete the challenges it gives you. Any problems, comment below!

ONLY READ THE FOLLOWING AFTER ATTEMPTING TASK 2

You got an error didn't you? Something about not being able to concatenate str (strings) and int (integers). Don't worry, this was supposed to happen. Getting an error seems scary at first but they are normal part of life for any programmer. They are often easy to understand and fix. Sometimes, its a spelling mistake, a missing bracket or something else trivial. The error message will say the category (in this case, TypeError) as where in your python file the error is occurring (in this case on or around line 150). If you can't figure it out, the best bet is to copy and paste the error message into a search engine. Almost always, someone else will have had the same error as you and will have posted about it on StackOverflow or GitHub issues. Some friendly individual will probably have written an explanation with some easy fix. Overtime, you will get fewer and fewer errors in your code, or at least get better at fixing them. You can also send me a message, just be sure to communicate exactly how I can recreate the error and what you were trying to do.

Python wont let you concatenate some variable types. What we have to do is convert (or cast) the integer to a string before doing the concatenation.

number_first_string = str(number_first)

In the above line, we are telling Python that we want to cast the integer variable number_first to the type str (string) and then assign it to a new variable called number_first_string.

Now go back to the Python file and try Task 2 again with the knowledge you have learned here.