User Tools

Site Tools


pyhang_game

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pyhang_game [2023/11/06 01:07] – created appledogpyhang_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.
  
 <Code:Python> <Code:Python>
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:
 +
 +<Code:Python>
 +import random
 +
 +wordlist = [
 +    "Dog", "Cat", "Bird", "Fish", "Elephant", "Tiger", "Lion", "Monkey", "Bear", "Horse",
 +    "Cow", "Chicken", "Pig", "Sheep", "Goat", "Duck", "Apple", "Banana", "Orange", "Grape",
 +    "Strawberry", "Bread", "Cheese", "Butter", "Milk", "Egg", "Rice", "Pasta", "Meat", "Chicken",
 +    "Beef", "Pork", "Fish", "Shrimp", "Salad", "Soup", "Sandwich", "Jacket", "Shirt", "Pants",
 +    "Dress", "Skirt", "Shorts", "Hat", "Shoes", "Socks", "Gloves", "Scarf", "Belt", "Coat"
 +]
 +
 +gameword = random.choice(wordlist)
 +gameword = gameword.lower()
 +
 +guessed = ""
 +badguess = ""
 +playing = True
 +check = ""
 +chances = 7
 +round = 1
 +
 +while playing:
 +    print("")
 +    print(f"Bad guesses: {badguess}")
 +    print(f"Guesses remaining: {chances}!")
 +    for letter in gameword:
 +        if letter in guessed:
 +            print(letter, end = ' ')
 +        else:
 +            print('_', end = ' ')
 +
 +    print("") # skip one line
 +
 +    guess = input("Choose a letter: ")
 +    guess = guess.lower()
 +
 +    if guess in gameword:
 +        print("Good guess!")
 +        guessed = guessed + guess
 +    else:
 +        print("Bad guess!")
 +        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
 +
 +</Code>
 +
 +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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki