Noadswood Science, 2014
To know the basics of Python coding and decoding Monday, September 07, 2015
IDE Using Komodo Edit (the integrated development environment) we are going to build two basics programs… To get started follow the usual procedure within Komodo Edit Open the program Click file new > new from template Choose the Python template Click open
IDE
Remember – there are three steps to follow when using an IDE program Enter the code > Save > Run print(“Hello, World!”) > save hello.py > run command hello.py *It is also really important to be careful of spacing and indenting when using Python > for now follow exactly the code shown! **Classic errors > upper and lower case Print and print; single and double quotes ‘’ and “”; minus and underscore - and _; and different brackets (), [], and {}
Error Busting Have you copied exactly the code to enter? Have you spelled everything correctly? Are there two quote marks (‘) around the expression you want to print Do you have extra spaces at the beginning of the line? Have you checked the lines above and below the highlighted lines? Have you asked someone else to check the code against what was asked? Are you using the correct programming language?
Turtle Python can draw code – in the code below we can ask it to draw a circle… Enter the code below into Python – try and determine what each part does… from turtle import * pendown() for n in range(24): forward(10) right(15) penup()
Turtle Python can draw code – in the code below we can ask it to draw a circle… Enter the code below into Python – try and determine what each part does… from turtle import * pendown() for n in range(24):(range starts a loop) forward(10) right(15)(turns the turtle clockwise 15 o ) penup()
Issue You may have noticed one significant issue here – as soon as the program is done it closes itself down… There is a simple script that can be used to keep the information on screen: - input("\n\nPress the enter key to exit.") The code above says display the statement “Press the enter key to exit” and if the key is pressed then the program will close
Ghosts The ghost game highlights some of the things to watch out for when writing programs in Python The game design is simple… A player must walk through a door (there are three choices) Behind one of the doors is a ghost If the door chosen doesn’t have a ghost the player continues to the next door If the door chosen does have a ghost then the game ends A score is collected for how many doors were passed before a ghost was encountered
Ghosts #Ghost Game*name of our program (human language) from random import randint print('Ghost Game') feeling_brave = True*ensure capitals are used score = 0 while feeling_brave: ghost_door = randint(1, 3) *this area needs to be indented by 4x spaces print('Three doors ahead...') print('A ghost behind one.') print('Which door do you open?') door = input ('1, 2, or 3?') door_num = int(door) if door_num ==ghost_door: print('GHOST!') feeling_brave = False else:*this area needs to be indented by 4x spaces print('No ghost!') print('You enter the next room.') score = score + 1 print('Run away!')*indents need to be removed print('Game over! You scored', score) input("\n\nPress the enter key to exit.")
Ghosts When the game is run you will see the following… The player must enter a number: 1, 2 or 3
Ghosts If there is no ghost behind the door then you see “No ghost!” You then get to continue…
Ghosts If there is a ghost behind the door then you see “GHOST!” The game is then over – it will tell you a score achieved (the number of times you went through a door without a ghost behind it)
Decoding The ghost game displays some of the key features of Python The code structure is very specific: Python uses spaces at the start of lines to work out which instructions belong together These spaces are known as indents For example, the code after while feeling_brave: is indented by four spaces to show it is all part of the main loop
Decoding Game set up Main loop Branching part Game ending
Decoding – Game Setup The instructions only run once – at the beginning of the game They setup the title, variables and the “randint” command (choosing the random number 1-3 Command: randint generates random numbers Command: print displays text when the game is run This resets the score to 0 #Ghost Game is a comment – this is not shown when the game is run
Decoding – Main Loop This loop tells the story and receives the player’s guess – it keeps on going as long as there isn’t a ghost behind the door that is picked When a ghost appears, the “feeling_brave” variables changes to “False” and the loop stops repeating Command: randint(1, 3) selects a random number between 1 and 3 Input asks for the player’s answer print commands display the text onscreen
Decoding – Branching Part This program takes a different path depending on whether or not there was a ghost behind the door that was picked If there wasn’t a ghost the player’s score is increased by one, but if there was a ghost, the “feeling_brave” variable is set to “False” This branch runs if there is a ghost behind the door the player picked If there is no ghost the player sees the message “You enter the next room” The score increases by one each time the player enters a room without meeting a ghost
Decoding – Game Ending This program takes a different path depending on whether or not there was a ghost behind the door that was picked If there wasn’t a ghost the player’s score is increased by one, but if there was a ghost, the “feeling_brave” variable is set to “False” The score is a variable – it will change depending on how many rooms the player passes through Input asks for the enter key to be typed – the screen will remain open until this is done print commands display the text onscreen