Question: Hi! I've pasted my code for a simple hangman game below. For the next step of my project I need to create a file to

Hi! I've pasted my code for a simple hangman game below. For the next step of my project I need to create a file to test this code using pytest and testing all three of my functions. I'm having trouble understanding how to test the def test_hangman() function. I've pasted my test code format below the actual hangman code. I know it's not indented correctly but chegg won't show the code properly for some reason. Thank you!

Project code:

import random

# Initial parameters to execute the game and greeting

def main():

print("Let's play hangman!")

print("You only have 5 tries, but I believe in you.")

print(" Good luck! ")

words_to_guess = ["ceramic",

"house",

"after",

"gift",

"please",

"about",

"tofu",

"angry",

"mound",

"sauce",

"cat",

"music",

"paint",

"choice",

"move",

"desk",

"greatly",

]

word = random.choice(words_to_guess)

limit = len(word)

count = 0

already_guessed = []

display = '_' * limit

hangman(word, limit, count, display, already_guessed)

# Loop to re-execute the game when the first round ends:

def play_loop():

play_game = input("Try again? y = yes, n = no ")

while play_game.lower() not in ["y", "n"]:

play_game = input("Try again? y = yes, n = no ")

if play_game.lower() == "y":

main()

elif play_game == "n":

print("Loser!")

exit()

# check input

def is_not_valid_input(guess):

return len(guess) == 0 or len(guess) >= 2 or guess <= "9"

# Initializing the conditions required to play

def hangman(word, limit, count, display, already_guessed):

if word == "_" * limit:

print("Winner!")

play_loop()

guess = input("I've chosen this word: {} guess a letter: ".format(display)).strip()

if is_not_valid_input(guess):

print("You gotta choose a letter. ")

hangman(word, limit, count, display, already_guessed)

elif guess in word:

already_guessed.extend([guess])

index = word.find(guess)

word = word[:index] + "_" + word[index + 1:]

display = display[:index] + guess + display[index + 1:]

print("{} ".format(display))

hangman(word, limit, count, display, already_guessed)

elif guess in already_guessed:

print("You already used that letter. ")

else:

count += 1

if count == limit:

print("So wrong, RIP hangman ")

print("The word was {} ".format(word))

play_loop()

elif count != limit:

wrong_sayings = [

"Nah! Guess again: ",

"Wrong! Guess again: ",

"Not right, guess again: ",

"Still wrong, guess again: ",

]

saying = random.choice(wrong_sayings)

print(saying + "You have {} chances left ".format(limit- count))

hangman(word, limit, count, display, already_guessed)

if __name__ == "__main__":

main()

hangman()

Test code format:

from project import play_game, is_not_valid_input, hangman

def main():

test_play_game()

test_is_not_valid_input()

test_hangman()

def test_play_game():

assert play_game("y") == True

assert play_game("Y") == True

assert play_game("n") == True

assert play_game("N") == True

assert play_game("i") == False

assert play_game("2") == False

def test_is_not_valid_input():

assert is_not_valid_input("0") == True

assert is_not_valid_input("word") == True

assert is_not_valid_input("23") == True

assert is_not_valid_input("a") == False

assert is_not_valid_input("e") == False

def test_hangman():

...

if __name__ == "__main__":

main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!