Question

In: Computer Science

in python This is the animal speaking game. Print the following menu for your user: ------------------------------------...

in python

This is the animal speaking game. Print the following menu for your user:

------------------------------------

1) Dog
2) Cat
3) Lion
4) Sheep
5) Exit

Please enter a selection.

------------------------------------

If the user enters 1, print("Bark")
If the user enters 2, print("Meow")
If the user enters 3, print("Roar")
If the user enters 4, print("Baah")
If the user enters 5, exit the program.

define functions for each animal. For example:

def dog() :
def cat() :
and so forth ...

When called, each animal function should return a string associated with the animal.

For example, dog() should return "bark" and cat() should return "Meow".

In the main() function, if the user selects selection 1 for dog, call the dog() function and print the return value "bark"

Add error checking code to determine if the user enters a menu number less than 1 or greater than 5. If so, tell the user the
correct range of menu selections is 1 - 5.

Be sure to put your name and assignment number at the top of the program in comments.

You should implement the following functions and call them from the main() function

- getUserInput() which prints the menu and returns the user input to the main() function

- dog()

- cat()

- lion()

- sheep()

Solutions

Expert Solution

#function to get user input
def getUserInput():
print("-"*30)
print("1) Dog")
print("2) Cat")
print("3) Lion")
print("4) Sheep")
print("5) Exit")
print("Please enter a selection")
print("-"*30)
inp = input() #get input
return inp #return input

#methods for various animal sounds
def dog():
return "Bark"

def cat():
return "Meow"

def lion():
return "Roar"

def sheep():
return "Baah"


#main method
def main():
while(True):
inp = getUserInput()
#check if input is ok
if(inp == "1"):
print(dog())
elif(inp == "2"):
print(cat())
elif(inp == "3"):
print(lion())
elif(inp == "4"):
print(sheep())
elif(inp == "5"):
break
else: #if not, display error
print("Correct range of menu selections is 1-5")

#calling main() when execution starts
if __name__ == "__main__":
main()

Code screenshot for indentation help:

Sample execution:

In case of any doubt, drop a comment and I'll surely get back to you.

Please give a like if you're satisfied with the answer. Thank you.


Related Solutions

print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
In Python, write a calculator that will give the user the following menu options: 1) Add...
In Python, write a calculator that will give the user the following menu options: 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Example 1: Let's calculate! 1) Add 2)...
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
On Python a) Use format() method to print an integer value entered by the user and...
On Python a) Use format() method to print an integer value entered by the user and its cube root with two decimal places. b) Print the same values as part (a) using format() function with keyword arguments and labels number and cubeRoot as in: format(number=n,cubeRoot=cr) c) Switch the order of keyword arguments and show that this has no effect on the output.
You must prompt the user to enter a menu selection. The menu will have the following...
You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Create a python program that will ask the user for his job title and print out...
Create a python program that will ask the user for his job title and print out the first letter, the last letter and the total number of letters in his job
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Build and present a menu to a user like the following and enclose it into a...
Build and present a menu to a user like the following and enclose it into a loop that ends when the Quit option is chosen. Scan the user's selection into an integer variable. 1. Enter user name. 2. Enter test scores. 3. Display average. 4. Display summary. 5. Quit. Selection: If the user selects Option #1, scan the user's name, and store it to a char array. Assume the user's name is under 20 characters. If the user selects Option...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT