2.3

## Raspberry Pi Foundation

Skip to 0 minutes and 1 secondNow that you've had a go at translating a Python programme into a Scratch programme, it's time to push yourself a little and see if you can turn a Scratch programme into a Python programme. I recommend that you use IDLE for this exercise, rather than using Trinket. This is because later on in the course, you'll be writing scripts that won't run inside Trinket, so it will be useful for you to become accustomed to the software now. Find IDLE in your application menu and open it up. The window that opens is called the shell, the interpreter, or sometimes the repl.

Skip to 0 minutes and 34 secondsIt doesn't really matter what you call it, but be aware that it's on different websites and tutorial videos, it could be called any of these things. The shell is used to write code that runs straightaway. This is useful if you just want to test out a line of code or find out the value of some variable. You can have a go at writing a few lines in the shell now. Try the following-- 2 plus 2, print 'hello,' x equals 100, x. When writing programmes, you don't write your code in the shell. Instead, you use a file. Click on File and then New File in the Menu bar to create a new Python file.

Skip to 1 minute and 23 secondsYou'll want to save this straightaway, as in IDLE, you'll always need to save prior to running any code. Click on File, and then Save, and call your programme usernamegenerator.py.

Skip to 1 minute and 46 secondsLet's see how you can write and run code in IDLE. In your usernamegenerator.py file, write the following two lines of code-- print 'no 3' and then print 'the large.' it doesn't really matter what you use inside the print statement, so you can write any strings you like. To run this code, you need to save the file. Either click on File and Save or alternatively, hold down the Control key and press S on your keyboard. To actually run the code, click on Run and then Run Module. Or alternatively, press the F5 key on your keyboard. You should now see the results of the programme output in the shell.

Skip to 2 minutes and 40 secondsNow let's try your first Python programme. Translating Scratch code into Python code helps learners avoid having to think about the logic of their programme, and instead, has them focus on the syntax of Python, which is so great way of introducing the language. For this exercise, you're going to make a username generator that will combine random words and numbers together to make up a unique username. Let's have a look at one version of the completed code for the username generator in Scratch, along with some of the data structures that are used. There's a link to the Scratch project in the text below this video, so you can have a play around with the programme.

Skip to 3 minutes and 18 secondsYour task is to turn this Scratch script into a Python script. So let's go back to the week one code snippets that show Python and Scratch code together to figure out how to progress.

Skip to 3 minutes and 31 secondsThe first thing you will need to do is to create a couple of lists containing adjectives and nouns. You'll need to type in the words yourself for now, but in the text below the video, there's some word lists that you can select from if you want. Then, you need to create two variables with values that are random words chosen from each of the two lists.

Skip to 3 minutes and 57 secondsNext, you want to create two variables with values that are random numbers between 0 and 9.

Skip to 4 minutes and 8 secondsAnd to finish off, concatenate, or join, the variable values together.

Skip to 4 minutes and 18 secondsThere are a couple of small tweaks you'll want to make. Firstly, it is necessary to change the numbers that are picked randomly to strings. We'll cover why in more detail later. But for now, you need to know that to change a number to a string, you can use the string method. So my_num equals str 6.

Skip to 4 minutes and 48 secondsAs you can see, the 6 is now changed into a string.

Skip to 4 minutes and 57 secondsSecondly, there's no need to write two lines of code to import the functions you need from the random module. So instead of writing "from random import choice" and then again, "from random import randint," this can be shortened to just one line. Simply put a comma between the two function names and that will import them both. Have a go at writing your usernamegenerator.py code and run it using F5 when you're done. If you get errors or are stuck, then just ask for help in the comments below. And don't forget to share your successes.

Picking unique usernames for websites and other software is never easy, so why not automate the process with a random username generator?

Now that you have had a go at translating a Python program into a Scratch program, it’s time to push yourself a little and see if you can turn a Scratch program into a Python program.

### Using IDLE

I recommend that you use IDLE for this exercise rather than using Trinket. This is because later on in the course, you will be writing scripts that won’t run inside Trinket, so it will be useful for you to become accustomed to the IDLE software now rather than later.

Find IDLE in your application menu, and open it. The window that opens is called the shell, or the interpreter or sometimes repl. It doesn’t really matter what you call it, just be aware that on different websites and tutorial videos, it could be called any of these things.

The shell is used to write code that runs straight away. This is useful if you just want to test out a line of code or find out the value of some variable. You can have a go at writing a few lines in the shell now. Try the following:

>>> 2 + 2
4
>>> print('Hello')
Hello
>>> x = 100
>>> x
100


When writing programs, you don’t write your code in the shell. Instead you use a file. Click on File and then New File in the menu bar to create a new Python file.

You’ll want to save this file straight away, since in IDLE you always need to save before running any code. Click on File and then Save, and call your program username_generator.py.

Let’s see how you write and run code in IDLE. In your username_generator.py file, write the following two lines of code:

print('No 3')
print('The Larch')


It doesn’t really matter what you use inside the print statement, so you can write any strings you like.

To run this code, you need to save the file again. Either click on File and then Save or alternatively hold down the Ctrl key and then press the s key on your keyboard.

To actually run the code, click on Run and then Run Module, or alternatively press the F5 key on your keyboard. (You might need to hold down the Fn key as well if your function keys are used for controlling volume and screen brightness.)

You should now see the results of the program as an output in the shell.

No 3
The Larch


Translating Scratch code into Python code helps learners avoid having to think about the logic of their program, and instead has them focus on the syntax of Python, so it’s a great way of introducing the language.

In this exercise, you’re going to make a username generator, that will combine random words and numbers together to make up unique usernames.

Let’s have a look at one version of the completed code for the username generator in Scratch, along with some of the data structures that are used.

And here is an image showing the data structures used and the main script:

Your task is to turn this into a Python script. Let’s go back to the Week 1 code snippets that showed Python and Scratch code together to figure out how to progress.

• The first thing you will need to do is to create a couple of lists containing adjectives and nouns. You’ll need to type in the words yourself for now. Here’s some lists of adjectives and nouns that you can select from if you want.

• Then you need to create two variables with values chosen at random from each of the lists.

• Next you want to create two variables with values that are random numbers between 0 and 9. Here’s how this can be achieved for values between 1 and 10.

• To finish off, concatenate (join) all the variables together.

There are a couple of small tweaks you will want to make.

• Firstly, it is necessary to change the numbers that are picked randomly to strings. This is because Python can only join a string to another string, and not to a number. We’ll cover this in more detail later, but for now you need to know that to change a number to a string you can do the following:
my_num = str(6)

• Secondly, there is no need to use two lines of Python code to import the code you need. You can simply write the following
from random import choice, randint

• Have a go at writing your username_generator.py code, and run it using F5 when you are done. If you get errors or are stuck, then just ask for help in the comments below, and don’t forget to share your successes.

You can see the full username generator project here. You can try out your new skills in a free Code Club — find out more at codeclubworld.org.