Thanks
#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 = {'frisbee':'Something that spins after being thrown',
'xbox':'A popular game console',
'school':'A place where you go to learn',
'hamster':'A pet',
'toyota':'A brand of cars and trucks'}
#Function that prints the COP 1000 line and briefly explains the game
def Intro():
print('COP 1000 Project 5 - Charles Goggins')
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.')
#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 types HELPME in the guess prompt, the program will provide a hint
def getHint():
hint = len(wordKey.index)
return hint
#Gets the first guess from the player, also ensures only characters are entered
def getGuess1():
print('Enter your first guess: ', end = '')
guess = input()
while True:
if guess.isalpha():
return guess
else:
if guess.isdigit():
print('Please enter a letter:', end = '')
guess = input()
#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():
print('Enter your second guess: ', end = '')
guess = input()
while True:
if guess.isalpha():
return guess
else:
if guess.isdigit():
print('Please enter a letter:', end = '')
guess = input()
#==============================================================MAIN PROGRAM===============================================#
#Initializes some varibles, ans = 'y' is for the play again loop
ans = 'y'
guesses = 0
while ans == 'y':
#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!')
if guessed != w0rd:
print('Sorry, that is incorrect')
#Counts the amount of guesses taken
guesses = guesses + 1
#Calls the getGuess2() function
guessed2 = getGuess2()
if guessed2 == w0rd:
print('Congratulations!')
else:
if guessed2 != w0rd:
print('Sorry, that is incorrect')
guesses = guesses + 1
#Code to provide the hint
if guesses == 2:
for i in scramWord:
print(w0rd[i])
#Code to ask the player if he/she wants to play again
print('would you like to play again?', end = '')
ans = input()

New Topic/Question
Reply



MultiQuote




|