In: Computer Science
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()
#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.