1 Replies - 1745 Views - Last Post: 02 December 2012 - 10:05 PM Rate Topic: -----

#1 Goggz56  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 25-March 11

Python school project - write a word scramble game. Status - Complete

Posted 29 November 2012 - 10:03 PM

Earlier today I asked for some help with my project.

Now my project is smooth and works like a charm. I've posted my program code below for those who need to do the same thing and need an idea of how to do it. Enjoy!

#Program name - COP 1000 Project 5
#Author - Charles Goggins
#Date - 11/8/2012
#Description: This program takes a word from a list and displays it to the
#player scrambled up. The player will have three chances to guess the word,
#after two incorrect guesses the program will provide the player with a hit
#to help him/her out.

#Algorithm:
#   1. Print COP 1000 header with introduction
#   2. Generate random word for scramblin'
#   3. Display scrambled word to player
#   4. Prompt player for guess #1
#       a. If guess incorrect, print sorry, incorrect message
#       b. If guess correct print good job message and go to step 7
#   5. Prompt player for guess #2
#       a. If guess incorrect, print sorry, incorrect message and provide hint
#       b. If guess correct print good job message and go to step 7
#   6. Prompt player for guess #3
#       a. If guess incorrect, print sorry, this is incorrect and provide word
#       b. If guess correct print good job message and go to step 7
#   7. Ask player if he/she would like to play again and return to step 1


#Imports the random module from the Python library
import random  

#Creates a dictionary for the words
words = {'lights':'Something with multiple colors on a Christmas tree',
         'santa':'Fat old man who comes around Christmas',
         'elves':'Little people with pointy ears that work with Santa',
         'tree':'The thing people put presents under on Christmas',
         'snow':'Frozen yet soft form of rain',
         'ornaments':'Accessories that get hung on a Christmas tree',
         'sleigh':'The vehicle that Santa rides in to bring toys to all the world',
         'christmas':'December 25th',
         'present':'A kid opens one on Christmas',
         'snowball':'The frozen and soft version of rain formed into a shape',
         'snowman':'Frosty the ________',
         'reindeer':'Rudolph the red-nosed _______',
         'jesus':'The person that Christmas is named for',
         'manger':'What Jesus was born in',
         'stars':'The bright lights in the sky at night',
         'grinch':'Green guy from Who-ville',
         'stockings':'Looks like a sock, people use these to hold small gift on Christmas',
         'rudolph':'Reindeer with a bright-red nose',
         'dasher':'One of Santa\'s reindeer',
         'garland':'Think tinsel for a Christmas tree'}


#Function that prints the COP 1000 line and briefly explains the game
def Intro():
    print('COP 1000 Project 5 - Charles Goggins\n')

    print('Word Scramble!\n')

    print('I\'m going to show you a scrambled word and give you three guesses.')
    print('After your second guess, I\'ll help by giving you a hint.\n')

#Selects a word from the dictionary at random and returns it it's caller
def getWord(word):
    wordKey = random.choice(list(word.keys()))
    return wordKey

#When the player guesses incorrectly twice, the program will provide a hing
def getHint():
    hint = len(wordKey.index)
    return hint

#Gets the first guess from the player, also ensures only characters are entered
def getGuess1():
    guess = input('\nEnter your first guess: ')
    while True:
        if guess.isalpha():
            return guess
        else:
            guess = input('\nPlease enter a letter: ')


#Does the same as getGuess1() but gets the player's second guess only if he/she didn't guess correctly the first time              
def getGuess2():
    guess = input('\nEnter your second guess: ')
    while True:
        if guess.isalpha():
            return guess
        else:
            guess = input('\nPlease enter a letter: ')

#Does the same as getGuess2() but gets the player's third guess only if he/she didn't guess correctly the first and second time
def getGuess3():
    guess = input('\nEnter your final guess: ')
    while True:
        if guess.isalpha():
            return guess
        else:
            guess = input('\nPlease enter a letter: ')

#When the player types y to the question and then types HELPME or HELP in the prompt, the program will provide a hint
def cheatCode(yes, werd):
    x = werd[0]
    y = werd[1]
    print(x, end = '')
    print(y)
    return x, y
#==============================================================MAIN PROGRAM===============================================#

#Initializes some varibles, ans = 'y' is for the play again loop
ans = 'y'
guesses = 0
while ans == 'y':
    Intro()
    #Calls the getWord() function
    w0rd = getWord(words)
    #Turns w0rd into a list to enable scrambling
    scramWord = list(w0rd)
    #Scrambles the word
    random.shuffle(scramWord)
    #Joins the scrambled word together to be able to display it
    sWord = ''.join(scramWord)

    print('The scrambled word is ' +sWord)

    #Calls the getGuess1() function and determines whether the player guessed correctly
    guessed = getGuess1()
    if guessed == w0rd:
        print('Congratulations!')
        ans = 'y'
    if guessed != w0rd:
        print('Nope, sorry')

        #Counts the amount of guesses taken
        guesses = guesses + 1
        cheat = input('Do you want help? [y/n] ')
        if cheat == 'y' or cheat == 'Y':
            cheatInput = input('Type "HELPME or HELP": ')
            cheatCode(cheatInput, w0rd)


        #Calls the getGuess2() function
        guessed2 = getGuess2()
        if guessed2 == w0rd:
            print('Congratulations!')
            ans = 'y'
            
        if guessed2 != w0rd:
            print('Nope, sorry')
            guesses = guesses + 1
            cheat = input('Do you want help? ')
            if cheat == 'y' or cheat == 'Y':
                cheatInput = input('Type "HELPME or HELP"')
                cheatCode(cheatInput, w0rd)
                    
                #Code to provide the hint
                while guesses == 2:
                    hint = words[w0rd]
                    print('Heres a hint ===> '+hint)
                    guesses = guesses + 1
                    
            guessed3 = getGuess3()
            if guessed3 == w0rd:
                print('\nCongratulations!\n')
                  
            else:
                if guessed3 != w0rd:
                    print('\nNope, sorry. The word was \''+w0rd+'\'\nBetter luck next time')
                    guesses = 0
                            
    #Code to ask the player if he/she wants to play again
    print('\nwould you like to play again?[y/n]', end = ' ')
    ans = input()
    if ans == 'n' or ans == 'N':
        print('\nGoodbye then')



Is This A Good Question/Topic? 0
  • +

Replies To: Python school project - write a word scramble game. Status - Complete

#2 npanagakis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-December 12

Re: Python school project - write a word scramble game. Status - Complete

Posted 02 December 2012 - 10:05 PM

Very neat, but this would probably belong in the snippets section.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1