Engineering Innovation Center

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Recitation 1 Programming for Engineers in Python.
PHP : Hypertext Preprocessor
An Introduction to Textual Programming
Introduction to Python
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Decision Structures, String Comparison, Nested Structures
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
The Basics Input / Output, and Primitive Data Types.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Linux Administration Working with the BASH Shell.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Chapter 4 Stacks
CST 1101 Problem Solving Using Computers
GCSE COMPUTER SCIENCE Practical Programming using Python
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Introduction to Computing Science and Programming I
Introduction to Python
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Formatting Output.
Matlab Training Session 4: Control, Flow and Functions
Intro to Python Programming – Part II
ECS10 10/10
Variables, Expressions, and IO
Shell Script Assignment 1.
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Intro to PHP & Variables
Agenda Control Flow Statements Purpose test statement
Engineering Innovation Center
Decision Structures, String Comparison, Nested Structures
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Python I/O.
Functions BIS1523 – Lecture 17.
File Handling Programming Guides.
Decision Structures, String Comparison, Nested Structures
Learning Outcomes –Lesson 4
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
We’re moving on to more recap from other programming languages
Do While (condition is true) … Loop
Coding Concepts (Basics)
Introduction to TouchDevelop
Margaret Derrington KCL Easter 2014
Introduction to C++ Programming
Computing Fundamentals
A look at Python Programming Language 2018.
Python programming exercise
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Computer Science
Beginning Python Programming
12th Computer Science – Unit 5
Programming with Python, for beginners
Input and Output Python3 Beginner #3.
General Computer Science for Engineers CISC 106 Lecture 03
Class code for pythonroom.com cchsp2cs
COMPUTING.
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Engineering Innovation Center Python I PopUp Course 2017

Python I Popup Course – Requirements and Notes You should already have Python (2.7) installed. If not please do so before continuing. This tutorial will be using a bash shell environment to type and run the code presented. You are free to use your favorite text editor or IDE to write up the python script but it is recommended that you still run the code in a shell environment.

Python I Popup Course – Hello World! To begin we will be creating the Hello World program in python. This is a standard, beginner program that prints the phrase “Hello World!” to the console. Start by creating a python file: vim helloworld.py Type the following line: print (“Hello World!”) Exit and save the file Open terminal to directory where your python script is located Run program: python helloworld.py Your output should match what you put in between the quotation marks!

Python I Popup Course – Python Hangman: Variables Think of all possible variables one would need to keep track of during a game of hangman. How many guesses do you want the user to have? What words would you want for the user to guess? How do you randomly pick a word from that list of words? (This is why we import random) How do you show the user the current game status? How do you tell the program that the game is over? How do you keep track of what letters the user has guessed?

Python I Popup Course – Python Hangman: Variables Python doesn’t need to declare the variable type when initializing variables. It is inferred by the variable’s contents. This is why we don’t need to say something like “int guesses = 10” Python knows that “guesses = 10” is a variable named guesses that has the integer value of 10. Thus guesses is an integer variable. Same with the gameOver variable. It is a boolean variable with the value of False

Python I Popup Course – Python Hangman: Variables Python lists are denoted by the square brackets. Each entry in the list is separated by a comma delimiter. You CAN mix types in lists. For example, I can have integer and string entries in the same list. Here, wordList is a list of three words, “car”, “cat”, and “doggo”. You can access these entries by their respective list index. The index starts at 0. wordlist[0] is car wordlist[1] is cat wordlist[2] is doggo

Python I Popup Course – Python Hangman: Variables On the 5th line of the code to the right, you can see we have a variable named “answer” but it may not be clear what the value of “answer” actually is. The value of “answer” uses the random python module to make a choice. The random module has functions that can be called by the user. In this case, we can call the “choice” function of the random module and pass in the wordList we created. This gives us a random choice of a word within our wordList. The actual value of “answer” is determined at runtime, or when you run the program. It will be different every time.

Python I Popup Course – Python Hangman: Variables Python is great for simple string and list manipulation. For gameProgress, we have a list containing a single underscore multiplied by len(answer). The python function len returns an integer representing the length of whatever was passed to the function. In this case, it will be multiplied by the length of whatever word is chosen to be the game’s answer. This will result in gameProgess being a list of underscores the same number of characters as the word chosen to be the answer. This is so we can show game progress to the user as unguessed letters can be shown with the underscores. We will then replace the underscores with guessed letters as the game progresses.

Python I Popup Course – Python Hangman: Variables With the last variable we have, we set it to an empty list. We do this in this case because we always want the initial list of letters guessed to be empty (because the user hasn’t guessed any letters in a brand new game!). We then add onto this lettersGuessed variable as the game progresses and because Python already knows the variable is a list, it makes our lives easier.

Python I Popup Course – Python Hangman: The Game Loop The main loop for the game logic will be within this while loop. This will loop as long as the given condition is true. In this case, while our guesses variable is not equal to 0 and the gameOver boolean variable is not true. In this loop, we need to do 4 things: Display current game information Get user’s guess input Check to see if user’s guess is correct and update all game info Check to see if the game is over

Python I Popup Course – Python Hangman: The Game Loop Display current game information We need to display all relevant information such as guesses remaining, what letters have been guessed, and what the current “status” of the word being guessed is at (i.e. _ a r) This can be accomplished with simple print statements. We will however need to typecast the integer variable guesses to a String type so that we can print this String concatenation of “Guesses left: “ and the number of guesses. This is done using the str() function Note: the character sequence “\n” is an escape sequence that will simply print a new empty line. This will be used for formatting purposes. There are many other useful escape character sequences.

Python I Popup Course – Python Hangman: The Game Loop Get user’s guess input At this point in the game, we need to get the user’s letter guess, but we also need to check to see if the user’s input is valid for our game. Getting the user’s input is easy and can be accomplished by using the raw_input() function. (See line 20) We now need to make sure the guess is only one letter, not a letter they have already guessed, and that it’s actually a letter and not a number or symbol. We will use a simple while loop to accomplish this input checking. While the input is not 1 character long or in the letters already guessed list or is not an alphabet letter, we will tell the user that their input is invalid and get new input, Once valid input is given, the loop will exit and the game will continue.

Python I Popup Course – Python Hangman: The Game Loop Check to see if user's guess is correct and update all game info If the userInput letter is in the answer word Then update the gameProgress We will use a for loop to iterate over every character in the answer Where the userInput letter matches the current character in the answer, we will replace that index of gameProgress. This is so the correctly guessed characters will be changed from “_” to the actual letter of the answer word. Add the userInput letter to the list of guessed letters (lettersGuessed) Decrement the amount of remaining guesses guesses -= 1 is the same as guesses = guesses - 1

Python I Popup Course – Python Hangman: The Game Loop Check to see if game is over If gameProgress does not contain any blank spaces (“_”) then that means the whole word was guessed. Tell the user they won and what the answer was Set gameOver boolean variable to true Else if gameProgress does contain blank spaces (“_”) and there are no guesses left, then the user has lost. Tell the user they lost and what the answer was This will cause the game loop to exit upon one of these conditions being met.

Python I Popup Course – Python Hangman: Running the script Terminal In a terminal, navigate to the directory where your script file is located. Run with command: ‘python <your filename here>.py’ IDLE If using an IDE for python such as IDLE, simply select “Run Module” under the “Run” menu Full python script is displayed on the next slide.

Python I Popup Course – Python Hangman: Final Result

Engineering Innovation Center 2017 – Jim Wilson