0% found this document useful (0 votes)
17 views10 pages

Word Game

This document describes a word game project that uses various Python libraries and functions. It defines the main function and several user-defined functions for the game, including functions to get a player's name, randomly pick a word, allow guessing of letters, and display the result. It also includes sections on the libraries and built-in functions used, a demo of the project, and tests of the functions.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Word Game

This document describes a word game project that uses various Python libraries and functions. It defines the main function and several user-defined functions for the game, including functions to get a player's name, randomly pick a word, allow guessing of letters, and display the result. It also includes sections on the libraries and built-in functions used, a demo of the project, and tests of the functions.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Word Game

Ameer Hamza
Contents

 Libraries and Built-in Function Used


 Defining and Calling Main Function
 User Defined Functions
 Project Demo
 Test Functions
 Test Functions Results
Libraries and Built-in Function Used

 Libraries
 cowsay Library
 Random Library
 Str Library

 Built-in Function
 choice
 title
 strip
 lower
 isalpha
Defining and Calling Main Function

def main():
test_player_name()
test_player_name()
test_result()
test_alpha_guess()

if __name__ == "__main__":
main()
User Defined Functions

def player_name(name):
welcome = f"Hi {name.strip().title()}, let's play a word game".replace(" ,",",")
print(welcome)
return name

def word_picker():
words = ['attic','battery','beach','board','bouquet','bruise','bubble','computer',
'condition','crust','cupcake','desert','dolphin','eagle','eclipse','farm','fireworks','fog',
'frog','geeks','giraffe','hawaii','hospital','igloo','japan','ketchup','koala','lemon',
'library','lion','mars','mathematics','mountains','panda','paris','player','programming',
'rainbow','reverse','salamander','science','scorpion','snowball','strawberry','summer',
'toothpaste','waffles','wasp','water','wealth','whisk','whistle']
word = choice(words)
return word
User Defined Functions (Cont.)

def word_guess(word):
if lose == 0:
attempt,i = 0,0
return True
dummy = ""
guess = alpha_guess(input("\nGuess
while attempt<10:
character: "))
lose = 0
dummy += guess
for i in word:
if guess not in word:
if i in dummy:
attempt += 1
print(i, end="")
print("\nWrong guess, try again")
else:
if attempt > 10:
print("_",end="")
return False
lose += 1
User Defined Functions (Cont.)

def alpha_guess(alphabet):
if answer==True:
alphabet = alphabet.strip().lower()
f_string = "You won"
if alphabet.isalpha() and
len(alphabet)==1: print("\n")

return alphabet print(cowsay.get_output_string(character,


f_string))
else:
return f_string
return "_"
else:
def result(word,answer):
f_string = f"You lost, the word is: {word}"
characters = ['beavis', 'cheese', 'cow',
'daemon', 'dragon', 'fox', 'ghostbusters', print("\n")
'kitty', 'meow', 'miki', 'milk', 'pig', print(cowsay.get_output_string(character,
'stegosaurus', 'stimpy', 'trex', 'turkey', f_string))
'turtle', 'tux']
return f_string
character = choice(characters)
Project Demo
Test Functions

def test_player_name():
assert player_name("Ameer") == "Ameer"
assert player_name("") == ""
def test_word_picker():
assert word_picker() in words
def test_result():
assert result("waffles",True) == "You won"
assert result("waffles",False) == "You lost, the word is: waffles"
assert result("farm",True) == "You won"
def test_alpha_guess():
assert alpha_guess("S") == "s"
assert alpha_guess("SS") == "_"
assert alpha_guess("s ") == "s"
Test Functions Results

You might also like