Functions revisited
1. A function is defined with a name and a set of parentheses followed by a colon
2. A def is a function
3. It also has a key word: def at the left side, no indentation
4. It should be defined right after any imports
5. If the function needs a value from another function, it should be inside the
parentheses. This is called the argument
6. If there is more than one argument, it is called an argument list
7. If there are no arguments, it is called an empty argument list
Hangman Game
HANGMANPICS = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | =========''', ''' +---+ | | O | /|\ | / \ | | =========''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split() def getRandomWord(wordList): # This function returns a random string from the passed list of strings. wordIndex = random.randint(0, len(wordList) - 1) return wordList[wordIndex]
print('H A N G M A N') missedLetters = '' correctLetters = '' secretWord = getRandomWord(words) gameIsDone = False
Classwork:
A. Write a short program, YI_printSpecial.py to display the shape on the screen using ‘ ‘ ‘:
___ / \ | | \ ___ /
B. Write a short program, YI_wordList1.py as follows:
1) Use the words list with
2) the split() function and
3) print all the elements of this list by using a “while” or “for” loop
Homework:
Write a short program, YI_wordList2.py with the following specifications:
1) Must contain a list of words
2) Use the split() function
3) Have a random integer from 0 to 9 to pick a word.
4) Display 3 random words by using a “while” or “for” loop.