How to code this program?

If I want the program to answer moo after I type cow, what's the code going to be like?


say in python...

2 Answers

  • 1 month ago

    Hi, aphantasic. Here's an example using a dictionary in Python:

    animal_sounds = { "cow": "moo", "sheep": "baa", "hen": "cluck", "pig": "oink" }

    while True:

    ... name = input ("Enter animal name (x=exit) : ")

    ... if name == "x": break

    ... for k, v in animal_sounds.items():

    ... ... if name == k:

    ... ... ... print (v)

    Don't type the dots (... ) - use a tab or spaces instead. I've just used dots to show indentation.

    • brilliant_moves
      Lv 7
      1 month agoReport

      @eddiej: you forgot the hens. They - along with the sheep and pigs are all thrown in for free. That's 300% extra free! What a bargain.

  • Anonymous
    1 month ago

    I'd use b*ll sh *t

Still have questions? Get your answers by asking now.