LOHKOISTA TEKSTIPOHJSIEEN OHJELMOINTIIN
SELKKARI ELI SCRATCH - PYTHON KÄÄNNÖSAPURI
SCRATCH JA PYTHON SYNTAKSI
https://www.futurelearn.com/courses/block-to-text-based-programming/2/steps/354634#fl-comments
One aspect of using a text-based language that many learners struggle with is understanding the specific syntax (the rules of the language) required. Mistakes made in a program are often due to these rules not being followed, and are called syntax errors. It is therefore helpful to show the parallels and differences between a language that a learner has already mastered and the one they are trying to learn.
Shown below are 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 upon assignment with a value.
- In Python, it is necessary to surround strings (any text) with either single (
'
) or double ("
) quotes.
Increment a variable
- In Scratch, a variable’s value can be increased or decreased.
- In Python, a variable’s value can be increased or decreased by reassigning it to itself with the addition or subtraction of a number.
Simple output
- In Scratch, you make a sprite talk to provide output to the user of the program.
- Python uses
print
statements to output to the shell. - Again in Python, you need to use single or double quotes if you are printing strings.
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.
- The example above isn’t the simplest way of doing this in Python though. Using the
while
loop, it is easier to check that the variable is less than10
.
while foo < 10:
print(foo)
Infinite loops
- Scratch has a specific type of loop that is infinite.
- In Python, a conditional loop is used that always evaluates to
True
.
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
, andelse
. Again colons (:
) and indentation are needed.
Testing for equality
- In Scratch, you can use the 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.
Lists
- Scratch lists are made in much the same way that variables are made.
- In Python, you use square brackets (
[]
) when creating a list, with commas between each pair of items.
- You can add to a list in both Scratch and Python.
- And you can also remove items from lists in both languages. In Scratch the first item in a list is at position
1
. In Python, however, the first item in a list is at position0
. That’s because in Python, you always start counting from0
.
Randomness
- Scratch has a
random
block that can be used to generate random numbers. - In Python, you need to import the
random
module.
- Both languages can also select random items from a list:
Concatenation
- Join strings together in Scratch using the
join
block. - In Python, you can use the addition operator (
+
) to join strings.
Indexing
- In both languages, you can find an item in a list or string using the item index.
Input
- You can collect user input in Scratch by using the
ask
block. - In Python you use the
input()
function.
Type casting
- In Scratch, strings and integers are intelligently detected. In Python, it is necessary to type cast when converting from one to another. For instance, you can change a string to an integer and back again as follows:
number = 6
number_as_string = str(6)
number_as_integer = int(number_as_string)
Cheat sheets for these comparisons are available at the bottom of this step in both digital and print formats.
Challenge
Have a look at the Python code below, then see if you can write the same program in Scratch.
name = input("What is your name?")
print("Hello " + name + ". It is nice to meet you")
age = input("How old are you?")
age = int(age)
print("You were born in "+ str(2017 - age))
Which parts of the program were easier to create in Python, and which parts were easier to create in Scratch?
Challenge
Have a look at the Scratch code below. Can you recreate it in Python?
A quick note on sharing
We love it when our learners share their code in the comments, either to get some constructive feedback or when helping others. However, the comments section is designed for commenting, not for displaying formatted code like this.
If you want to share your Scratch scripts, then you can share the link to your code. Similarly, if you are using Trinket in the first few weeks of the course, then just create links to the code you want to share.
If you are using Python that has been installed on your computer, then the best way to share your code with others is through a sharing service. The one that most software developers use for simple code snippets is GitHub Gists. You can create an account, and then have as many private or public gists as you like. An even simpler solution is to use a pasting service such as Pastebin.
ERILAISIA JUTTUJA :)
https://www.futurelearn.com/courses/block-to-text-based-programming/2/steps/354639
You can use this passage for your game if you like, or come up with your own version. Now that we have the basic text, we can replace some of the words, such as nouns and verbs. Here’s an example.
Ice cream is a frozen dessert made from plural noun ,with added flavours and sweeteners. This mixture is quickly frozen while it is verb ending in ed ,so that large plural noun do not form. Some ice cream is made with noun ,extracted from seaweed, so that it is not sticky. There are many different flavours of ice cream, such as food and food . Ice cream often has things added to it for flavour, like noun, noun , ornoun.
Now let’s look at how you could use Python to create a Mad Libs–style game. The algorithm is fairly simple:
- Ask for the user to input some words, such as plural nouns and foods
- Assign these inputs to variables
- Insert these variable values into the strings to complete the paragraph
In Python, asking the user for input and assigning the value to a variable can be accomplished in a single line:
answer = input('What is the meaning of life? ')
You can then use this variable in a print
statement using simple string concatenation.
print('The meaning of life is ' + answer + '.')
So, to make the Mad Libs game, you can begin by asking the user for a load of words, and finish by inserting them into some strings that will be printed.
Here is the code for a complete Python program, and below that is a Trinket version for you to test out.
noun_1 = input("Give me a plural noun ")
verb = input("Give me a verb ending in ed ")
noun_2 = input("Give me another plural noun ")
noun_3 = input("Give me a noun ")
adjective = input("Give me an adjective ")
food_1 = input("Give me a type of food ")
food_2 = input("Give me another type of food ")
noun_4 = input("Give me a noun ")
noun_5 = input("and another one ")
noun_6 = input("Give me one last noun ")
print("Ice cream is a frozen dessert made from " + noun_1 + ", with added flavours and sweeteners")
print("This mixture is quickly frozen while it is " + verb + ", so that large " + noun_2 + " do not form.")
print("Some ice cream is made with " + noun_3 + " extracted from seaweed, so that it is not " + adjective)
print("There are many different flavours of ice cream, such as " + food_1 + " and " + food_2)
print("Ice cream often has things added to it for flavour, like " + noun_4 + ", " + noun_5 + " or " + noun_6)
There are only three programming constructs used in the game:
1. Asking for input from the user and assigning variables to that input 2. Outputting strings 3. Concatenating stringsNow see if you can recreate this program using Scratch.
Ask for help in the comments if you become stuck, and once you have completed your program, you can share your creation by adding a link to the project.