CODE1
CODE1
CODE1
Make Your Own Fun
• PYTHON IS A GREAT LANGUAGE TO HAVE ON Z
• 1 IF YOU WANT TO CODE, THEN …
• 2 LET’S SEE WHAT’S INSIDE
• 3 A WORD ON EXTENSIONS
• 4 A ROSE BY ANY OTHER NAME
• 5 A BIT OF AN ANTICLIMAX ?
• 6 BLOCKS FOR THE BETTER
• 7 OR ELSE!!
• 8 DON’T LOSE YOUR MARBLES
• 9 THE FINAL COUNTDOWN
• 10 I’M A VISUAL LEARNER
• 11 ALL OUT OF MARBLES
• 12 DOUBLE-CHECK; SUBMIT
IBM Z Xplore
PYTHON IS A GREAT LANGUAGE TO HAVE ON Z
The Challenge
You’ll be introduced to the basic concepts of coding here. Or, if you’re already familiar with
variables, loops, and ‘if’ statements, you’ll be quickly getting a refresher and learning that it
doesn’t matter what platform you’re on, Python code looks like Python code.
You’ll be using the Python programming language, as it’s perhaps the most widely-used language for new
development right now, and it makes it very easy to get started with simple code that won’t overwhelm
you if this is your first time coding.
CODE1|230422-1718
All you need to get started is access to the USS shell, either through an SSH program, or a terminal
window.
The only technical challenge you need to have completed before this one is VSC1.
Investment
Steps Duration
12 120 minutes
CODE1|230422-1718
SSH into the IBM Z system and check your home directory for a file called “code1.py” (using the ls
command); if you do not have one, copy the original code1.py program from /z/public, either by copy-
and-pasting through VSCode, or by entering the following command:
cp /z/public/code1.py ~
python3 code1.py
This says “Use the Python interpreter to run the code1.py program in this directory” and look at the
output.
As you can see, there are lot of things happening in here; in the next step, you will take a look
“under the hood” and see what’s going on inside.
There is a small amount of code to look at in here, and lots of comments (those are the lines in green
starting with #)
CODE1|230422-1718
Even if you have never programmed before, you can see the part where it
You may find some of this information helpful as you tackle later steps. (that’s a hint, right there!)
You might get a message from VSCode asking if you want to install the Python extension.
CODE1|230422-1718
If you do have a Python extension installed, you can try to keep using it, but be aware you will
probably get warning messages in later Python-related challenges.
One to look out for in particular is an error messge telling you that the ‘zoautil_py’ module cannot be
found – this is NOT a problem for you working on these challenges.
If you want your editor to look like the screenshots here, don’t install any extensions besides the
ones outlined in the challenges.
CODE1|230422-1718
This Python program takes a word and figures out if the letter “z” is in it.
A variable is a placeholder for a value in a program, and it can be letters, numbers, or really any
sort of data.
In this case, it is used to represent the word you want to “investigate” - to check if it contains a
“z”.
The program also has a variable for holding the value of the particular letter you use for the
investigation.
CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [7/20]
5 A BIT OF AN ANTICLIMAX ?
Nothing happens, right?
CODE1|230422-1718
Edit the code so instead of “pumpkin” the variable word is set to “pizza”, save the file, and run the
program again in your USS terminal session.
You should see a message confirming that “pizza” does in fact have the letter “z” in it.
Feel free to experiment with other words, and letters, and then look at the part of the code that does
the checking.
Note: Make sure to save your file between edits (by going using the editor menu option File -> Save, or
using the keyboard shortcut) to commit any changes you make, and remember to the run the code in your
USS terminal session.
Program code can generally be organized into blocks of code that can be run in order, or if a condition
is met.
CODE1|230422-1718
This program uses an ‘if’ statement on line 13.
An ‘if’ statement has the condition being tested, followed by a colon ( : ) and the code which will be
run, when that condition is satisfied, is in a “block” underneath it.
The block of code is “indented” (moved to the right), so you know that anything in that block is what’s
going to happen if the condition is met.
C and C++ are good for system utilities and high-performance applications that benefit from lots of
control over memory and other system resources.
Python is an extensible language that has been around for a long time and used for a lot of
different tasks. You can call it a General-Purpose Programming Language, as you can use it for
• AI (Artificial Intelligence)
• mathematical simulations
• web applications
CODE1|230422-1718
• games
• automation
• just about anything.
If you’re going to learn just one programming language, it makes sense for it to be Python.
From there, once you have the fundamentals understood, it’s just a matter of learning any other
programming language’s particular points.
Code on!
CODE1|230422-1718
Lines 15 and 16 in code2.py add another step to the ‘if’ statement - an ‘else’ clause.
If the condition being tested on line 13 is not met (there is no ‘z’ in the word) the program can say
or do something about that.
Right now, these lines of code are commented out, so they do not actually do anything.
Delete the hash marks ( # ) at the beginning of the lines in the ‘else’ block, save the file, and then
run it again.
Make note of the indentation - the ‘else’ should line up with the ‘if’, and the next line (or block or
lines) should be indented to show that it belongs to the ‘else’ above it.
Test it out and make sure it works for ‘z’ and non-‘z’ words.
CODE1|230422-1718
An important thing to notice is how the message about how many marbles are left appears in the code
only once but gets printed out 10 times.
In this case, the condition is true as long as there are more than 0 marbles left.
That looks like a special feature that you can enable by uncommenting.
Erase the comment marks so the code is active and run the program again.
The code was supposed to issue a warning message when there are three or fewer marbles left, but
something is wrong with the logic in that ‘if’ statement.
OH NO!!!
CODE1|230422-1718
See if you can fix it so the message prints out just like the screenshot above - only when there are 3
or fewer marbles left.
Hmmm…
There are many different ways you can fix this code.
CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [14/20]
“HOW ELSE CAN I CONTROL CODE?”
We’re really just skimming the surface of Python programming in this challenge, enough to give
someone with no programming experience a chance to check out what coding looks like with lots of
examples.
If you are keen and want to learn more, be sure to read more about what you can do with Python at
IBM Developer .
There are videos, tutorials, podcasts, code patterns, and lots of projects you can get started on,
no matter what your current skill level is.
Even if you don’t plan to be a fulltime coder, it’s important to have some familiarity with code so
you can fix problems when they come up, and maybe even add features to code someone else wrote.
CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [15/20]
10 I’M A VISUAL LEARNER
Numbers are nice, but sometimes it is better to visualize things.
Your next task is to make the program print out the number of marbles left as asterisks. So when
there’s 5 marbles left, you print *****
One of the variables in the program ‘marble_dots’ is just ten asterisks in a row.
The following line of code will make the program print out six marble dots.
CODE1|230422-1718
print(marble_dots[:6])
If you replace the ‘6’ with a variable name, it will print out the number of asterisks for whatever the
variable is set to. (Assuming the variable has been set to a number!)
CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [17/20]
11 ALL OUT OF MARBLES
CODE1|230422-1718
The user needs to know when the program is done, and that there are no more marbles left.
Make the message say “You are all out of marbles”, make sure it appears last, and only once in your
program.
Make sure you have the right number of “marbles” displayed, that the spacing looks right, and that your
final “all out of marbles” message looks just like it does in the screenshot.
CODE1|230422-1718
The validation job will run your ‘marbles.py’ program, so make sure it’s all working as described.
This is just the beginning; you’ll do even more with Python in later challenges, so hopefully you’ve
enjoyed this.
In most things, the hardest part is getting started. If this was yourfirst time
hacking away at code, it might have felt difficult,frustrating, and even a little
weird.
We picked an example that would illustrate a few of the most important concepts: + Wrapping up
variables + loops + if/then/else statements + modifying variables + using library Fundamentals
functions.
Completing this challenge means you’re able to envision a problem, mentally solve
it, and then implement the solution in code.
CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [20/20]