Question

HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUYS HELPS ME OUT ON HOW TO TEST A SIMPLE CODE SINCE ITS A GUESSING GAME! THANK YOU. PYTHON

import random # here it allows us to use the benefits of the functions random #function 1 which is just a screen organized wodef hangman): #random picks one of these randomly word - random.choice([crocodile, lion, cheeta, giraff, monkey, ma

import random # here it allows us to use the benefits of the functions random #function 1 which is just a screen organized wordings def screen): screeninstructions () name input("Your Name : ") print('Welcome, name, 'This is a simple guessing game') print( print( 'Now', name, 'let the guessing begin with only 9 tries') print('make them count, lets goooo') #calling out the function hangman() print() #second functions that tells the person or the player a random fact as a treat of guessing correct def randomfact(animal): if animalcheeta' print("did you know that cheetas reaches their max speed which is 65mph") elif animal == 'lion' : print("a lion's claws can reach lengths of up to 1.5 inches" elif animal 'crocodile': print("the smallest crocodile species is the dwarf crocodile which can be 5 feet in length and weigh up to 40-70lb" elif animalgiraff: print( "Giraffe's can eat 70-80 pounds of leaves a day and feed 16-20 hours" elif animalmonkey: print("There are currently 264 known monkey species, but there are others to discover!") elif animal'zeebra": print("Did you know that every zebra has a unique pattern of black and white stripes" elif animal "deer print("Each year, antlers fall off and regrow" elif animalpenguin': print("The fastest species is the Gentoo Penguin, which can reach swimming speeds up to 22 mph") elif animal 'Magpies print("Magpies Dont Like Shiny Things - They are Scared of Them because that is how their survival nature taught the elif animalparrot' print( "Parrots are intelligent birds and like us humans they have feelings and can get sad and throw tantrums, fun elif animal == 'camel print( "Camels rarely sweat, even when ambient temperatures reach 49 °C and their title is The Ship Of The Desert" elif animal == ' leapord. : print("Sadly Leopards are predominantly solitary animals that have large territories and they fight to the death" elif animalgazelle: print("The tiny Thompsons gazelle's exhibit the very distinctive behavior of stotting which is jumping up to 10 fee #a third function of the screeen before entering the game. explains the guesser what will happen screeninstructions(): print('instructions:') print('you will have 9 trials to guess what word has been picked') print('each mistak will cause you to lose a limb, be careful!') print( 'once the correct asnwer is found, you wil1 learn a random fact about the animal') #main function which is all the acton happens.
def hangman): #random picks one of these randomly word - random.choice(['crocodile', 'lion', 'cheeta', 'giraff', monkey', 'magpies' 'zeebra', 'deer leapord', gazelle','parrot']) #choosing randomly strictly only Letters letters = 'abcdefghíjk1mnopqrstuvwxyzABCDEFGHÍJKLMNOPQRSTUWXYZ #setting turns to 9 and slowly decreasing it to penguin', 'camel', turns 9 guessed- .. #Letters that are used by the player will be added here #conditional to keep going if input is still empty while len (word) empty_string #missed trials which are zero then increased by 1 each time the guess is wrong missed #conditional if word is one of the Letters then add the Letter to the empty string for letter in word: if letter in guessed: empty stringempty string + letter else: empty_string empty_string ''+' missed -missed + 1 #where the missed trials are increased after each attempt #if it is a word from the list then print the Letter which is all the Letters guessed if were correct if empty_stringword: print (empty_string) #funfact about the animal they guessed print("") print( 'Marvelous!you are correct! the word was', word) rint() print('fun fact about', word) print() #calling the function randomfact onto word randomfact(word) break #stop the Loop if his right print("Guessing Time! : ", empty-string) # other wise keep guessing guess input().lower() #all ípnuts are lower cased even if user put upper case # if not a Letter then will say invalid if guess in letters: guessed-guessed guess else: print('Enter a valid letter!) guess input().lower() # again lower case every letter or input #if guessed incorrect then decreas his turns by one if guess not in word: turns = turns - 1 if turns4: print("nice try but again") print ) if turns3: print("come on think about it a little bit") print( o)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In Python we use assert statement as:

  1. assert statement has a condition and if the condition is not satisfied the program will stop and give AssertionError.
  2. assert statement can also have a condition and a optional error message. If the condition is not satisfied assert stops the program and gives AssertionError along with the error message.

In your program call a variable named assert animal, it means that if that the animal entered by user is not found in your program then the program will return an error message or an assertion message.

I am providing an example below for the same:-

def avg(ranks):

    assert len(ranks) != 0

    return round(sum(ranks)/len(ranks), 2)

ranks = [62, 65, 75]

print("Average of mark1:",avg(ranks))

It will produce an output of 67.5, but if we enter an empty list

def avg(ranks):
    assert len(ranks) != 0
    return round(sum(ranks)/len(ranks), 2)

 
ranks = []
print("Average of mark1:",avg(ranks))

Then it will produce an assertion error.

It works same as Exceptional Handling works for JAVA

Add a comment
Know the answer?
Add Answer to:
HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT