Download presentation
Presentation is loading. Please wait.
1
Engineering Innovation Center
Python I PopUp Course 2017
2
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.
3
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!
4
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?
5
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
6
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
7
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.
8
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.
9
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.
10
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
11
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.
12
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.
13
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
14
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.
15
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.
16
Python I Popup Course – Python Hangman: Final Result
17
Engineering Innovation Center
2017 – Jim Wilson
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.