## Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.
1.6

## Raspberry Pi Foundation

Skip to 0 minutes and 3 seconds When programming using a text-based language like Python, it won’t be long before you encounter an error message like this one. Syntax error. Every programming language has a different set of rules called syntax. And this message is telling me that I have made a mistake in my program by not following these rules. In this case, I haven’t included a second quote. Now, to help your learners transition from block to text-based programming, it can be helpful to compare and contrast the differences in syntax. Now let’s look at some examples. So first, when assigning a variable, in Scratch you need to make a variable before you can assign or set its value.

Skip to 0 minutes and 51 seconds Whereas in Python, a variable is created as soon as a value is assigned to it. Also in Python, you must enclose strings or text in single or double quotes. Now to increment a value, in Scratch, we can use the change block to either increase or decrease a block’s value. Whereas in Python, a new value has to first be calculated by adding or subtracting an amount to the value. And then that new value is assigned back to the variable.

Skip to 1 minute and 30 seconds To output to the screen– in Scratch, you can make a sprite talk by using the say block. An equivalent in Python would be the print statement, which will output text, numbers, or symbols to the screen. But again, we have to remember to use single or double quotes when using strings or text. Now one of the biggest differences is when using conditional loops. In Scratch, a conditional loop repeats until a statement is true. Whereas in Python, a conditional loop repeats as long as a statement is true. So if we compare these two, repeat until score is greater than 10. Keep running this code until the variable score is greater than 10.

Skip to 2 minutes and 19 seconds Whereas in Python, we would say, while score is less than or equal to 10, keep running this code. Also note how Python requires a colon at the end of the statement and how a code that is part of the loop is indented. And we can compare this to the way that the say block is indented as part of the repeat until block in Scratch. Now in Scratch, there is a specific type of block for doing infinite loops. It’s called a forever block. And in Python, there is no equivalent, no specific forever block. But a conditional loop is used which always evaluates to true. So while True, run this code is equivalent to forever.

Skip to 3 minutes and 5 seconds When using selection in Scratch, there are two selection blocks, an if and an else. And Python has a similar syntax. If this condition run this code, else run this code. And we can note, again the use of a colon at the end of the statement, and indentation to denote the code which is part of this condition. However, in Scratch, if multiple conditions are required, if statements have to be nested, one inside the other. Whereas in Python, there are three conditional statements– if, elif, and else. And, again note how colons are used and indentation.

Skip to 3 minutes and 50 seconds Finally, in order to be able to test equality, in Scratch we can use an operator block and a single equals sign. So we can say, score is equal to 10. Now in Python, a single equals sign is reserved for variable assignment. So a double equals sign is used to test for equality. Score is equal to 10.

Skip to 4 minutes and 14 seconds Now it’s time for a challenge. Take a look at this Python program. It’s featured in the article below. And see if you can write the same program in Scratch. Were there parts of the program which you thought were easier to create in Scratch? Share your thoughts and your Scratch programs by pasting a link to your program in the comments.

# Scratch and Python syntax

One aspect of text-based languages that many learners struggle with is understanding the specific syntax (the rules of the language) required. Mistakes in a program are often due to not following these rules, and these mistakes are called syntax errors. Therefore, it is helpful to show the parallels and differences between a language that a learner has already mastered and the one they are trying to learn.

In the next two steps I’ll show you a few Scratch blocks and their equivalent code in Python. The list is far from exhaustive, and is intended as a reference guide rather than an exercise to be worked through.

### Variable assignment

• In Scratch, a variable needs to be created before it can be assigned a value, whereas in Python, a variable is created when a value is assigned to it.

• In Python, it is necessary to surround strings (any text) with either single (') or double (") quotes.

score = 0
welcome = 'Welcome to the game'


### Increment a variable

• In Scratch, a variable’s value can be increased or decreased. To do this, you use a change block with a variable and the number to increment by, using a negative number to decrease the value.
• To increase or decrease the value of a variable in Python, you must calculate the new value using the value stored in the variable, and assign this new value to the variable.

score = score + 1


### Simple output

• In Scratch, you make a sprite talk to provide output to the user of the program.
• Python uses print statements to output text, numbers, or symbols.
• Again in Python, you need to use single or double quotes if you are printing strings.

print('Your score is amazing!!!')
print(score)


### Conditional loops

• Scratch’s conditional loop repeats until a certain statement is True.
• Python’s conditional loop repeats as long as a certain statement is True.
• There needs to be a colon (:) at the end of the statement in Python.
• Notice that the code that is inside the loop is indented. Indentation is normally four spaces or a single tab. This can be compared to the way the Scratch conditional loop block brackets the code within it.

while not score > 10:
print(playing)

• The example above can be simplified in Python though. Using the while loop, it is easier to check that the variable is less than or equal to 10.
while score <= 10:
print(playing)


### Infinite loops

• Scratch has a specific type of loop that is infinite.
• In Python, a conditional loop is used that always evaluates to True.

while True:
lives = lives + 1


### Conditional selection

• Scratch has two selection blocks that can be used. If multiple conditions are required, they need to be nested within each other.
• Python has three selection statements: if, elif, and else. Again colons (:) and indentation are needed.

if score > 10:
print("Well done you are doing a great job!")


if score > 10:
print ("Well done you are doing a great job!")
else:
print ("Keep going try to get to 10 points!")


if score > 10:
print ("Well done you are doing a great job!")
elif score < 10:
print ("Keep going try to get to 10 points!")
else:
print ("Yes! You have made it to 10 points!")


### Testing for equality

• In Scratch, you can use a operator block with an equal sign (=) to test if one value is the same as another value.
• In Python, a single equal sign is reserved for variable assignment, so a double equal sign (==) is used to test for equality.

score == 10


### Challenge

Have a look at the Python code below, then see if you can write the same program in Scratch using the blocks you have seen in this step.

score = 0
welcome = "Welcome to the game"
print(welcome)
while score <= 20:
score += 2
print("You get 2 bonus points for being under 20!!!")
if score >= 20:
print("Well done you have completed the game!")
else:
print("You need to get to 20 to complete the game")


Were there parts of the program which you thought were easier to create in Scratch?