pyhang_game
                Differences
This shows you the differences between two versions of the page.
| pyhang_game [2023/11/06 01:07] – created appledog | pyhang_game [2024/07/29 00:06] (current) – appledog | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| * Hangman | * Hangman | ||
| - | == Hangman Game | + | == Hangman Game v1 | 
| - | The following game illustrates the use of lists. | + | The following game illustrates the use of lists. It is one way to do the game. | 
| < | < | ||
| Line 61: | Line 61: | ||
| Initially, you can just use print(gameword) instead of a more complex way of printing the game word. Later you can introduce .join() and explain what it does. Also, after the testing stage you can add more words. | Initially, you can just use print(gameword) instead of a more complex way of printing the game word. Later you can introduce .join() and explain what it does. Also, after the testing stage you can add more words. | ||
| + | |||
| + | == Version 2 | ||
| + | I taught another course to older kids and came up with a more straightforward way: | ||
| + | |||
| + | < | ||
| + | import random | ||
| + | |||
| + | wordlist = [ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | |||
| + | gameword = random.choice(wordlist) | ||
| + | gameword = gameword.lower() | ||
| + | |||
| + | guessed = "" | ||
| + | badguess = "" | ||
| + | playing = True | ||
| + | check = "" | ||
| + | chances = 7 | ||
| + | round = 1 | ||
| + | |||
| + | while playing: | ||
| + | print("" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | for letter in gameword: | ||
| + | if letter in guessed: | ||
| + | print(letter, | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | print("" | ||
| + | |||
| + | guess = input(" | ||
| + | guess = guess.lower() | ||
| + | |||
| + | if guess in gameword: | ||
| + | print(" | ||
| + | guessed = guessed + guess | ||
| + | else: | ||
| + | print(" | ||
| + | badguess = badguess + guess | ||
| + | chances = chances - 1 | ||
| + | |||
| + | # Check to see if we need to keep guessing | ||
| + | playing = False | ||
| + | for letter in gameword: | ||
| + | if letter not in guessed: | ||
| + | playing = True | ||
| + | |||
| + | # End the game if we run out of chances | ||
| + | if chances <= 0: | ||
| + | playing = False | ||
| + | |||
| + | </ | ||
| + | |||
| + | This version began with a wordlist and random.choice. Then we made a guess-check algorithm. Then we replaced the print statement with the one that prints _'s. Then we made the game loop. Then we added checks for chances and completion. | ||
pyhang_game.1699232833.txt.gz · Last modified: 2023/11/06 01:07 by appledog
                
                