3.2.5.9 Lab – Writing Simple Python Scripts (Answers)

3.2.5.9 Lab – Writing Simple Python Scripts (Instructor Version)

3.2.5.9 Lab - Writing Simple Python Scripts (Answers) 3

Objectives

  • Use PL-App to access the Raspberry Pi
  • Create a new Python application
  • Create a simple game using Python

Background

Python is one of the most commonly used languages for developing new rapid prototyping solutions. It can be used to create web applications, desktop applications, and more. Python is also extensively used in Data Science to analyze Big Data.

Python is an interpreted computer language, and does not require compiling. It is platform independent and can run on Linux, Windows, OsX, and other systems. Python programs can be written using any text editor and should have the extension .py.

This lab provides a basic introduction to programming Python 2.7.x using the PL-App

Required Resources

  • PC with Internet Access
  • Raspberry Pi with power cable and either a wired or wireless network connection
  • Raspberry Pi that is configured and imaged for PL-App access

Part 1: Writing simple Python scripts in a notebook

Step 1: Code cell

1. Enter the following sample code into the cell below. This code assigns numerical values to the variables x and y, and then compares the two numbers.

Note: Pay special attention when indenting. In Python, the specific blocks of code are identified by indentation. In this example, the “print” functions are called from within the “if” statements.

x = 1
y = 2
if (x > y):
  print ("x is bigger than y")
else:
  print ("y is bigger than x")

Step 2: Execute the code in the cell

1. In the code cell, click on the Play button at the top left corner of the cell. The Python code will be executed on the Raspberry Pi and the output displayed here in the notebook.

3.2.5.9 Lab - Writing Simple Python Scripts (Answers) 4

Step 3: Create a Simple Game using Python

1. In this step, your task is to create a Python Application called Guess the number.

2. Enter the following lines of code in the code cell below. Pay special attention to indentation. The block after the “while” function is indented to identify the lines which are part of this loop cycle. The “print” functions inside of the “if” statements are also indented, thus identifying blocks of code executed once the statement in the “if” is true.

import random, math
random.seed()
x = math.floor(random.random()*1000)+1
z = 0
b = 0
while x != z:
b=b+1
z = int(input("Guess My Number Between 1 and 1000: "))
if z < x: 
     print("Higher!") 
if z > x:
     print("Lower!")
print("Correct! " + str(b) + " tries.")

3. In the code cell, click on the Play button at the top left corner of the cell. The Python code will be executed on the Raspberry Pi and the input and output displayed here in the notebook.

4. To stop the code, click the Stop button to stop the program.

Reflection

What is the maximum number of guesses required to win “Guess the Number” if you started at one, counting up? What is the maximum number of guesses required if you started at 500 and divided the range of possible answers by half using each guess?

Try to implement a cheat code – a magic number that if entered, it reveals the secret number, while continuing to run the application. Since the secret number will be revealed, you now have a good chance to win the game!

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x