Професионална програма
NKKalev avatar NKKalev 10 Точки

Python - Wolf in Sheep's Clothing - More Exercises: Basic Syntax, Conditional Statements and Loops


Може ли идеи/решения за тази задача:


Wolves have been reintroduced to Great Britain. You are a sheep farmer, and are now plagued by wolves which pretend to be sheep. Fortunately, you are good at spotting them.

Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing at the front of the queue which is at the end of the array:

[sheep, sheep, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE)

     4          3                    2           1

If the wolf is the closest animal to you, print "Please go away and stop eating my sheep". Otherwise, return "Oi! Sheep number N! You are about to be eaten by a wolf!" where N is the sheep's position in the queue.

Note: there will always be exactly one wolf in the array.


The input will be a single string containing the animals separated by comma and a single space ", "





sheep, sheep, wolf

Please go away and stop eating my sheep

wolf, sheep, sheep, sheep, sheep, sheep

Oi! Sheep number 5! You are about to be eaten by a wolf!


Не мога да я измисля. След дълго ровене в интернет успях да достигна до следното:


def warn_the_sheep(queue):
    the_wolf_position = queue.index("wolf")
    if the_wolf_position == 0:
        return 'Pls go away and stop eating my sheep'
        return "Oi! Sheep number " + str(the_wolf_position) + "! You are about to be eaten by a wolf!"


но не разбирам как точно работи, а и не получавам никакви резултати в PyCharm.

Fundamentals Module
l.markov avatar l.markov 1 Точки

Ето още едно предложение за решение:

animals = input()

animals_list = []
for animal in animals.split(","):

farmer_position = len(animals_list) + 1

if animals_list[-1].strip() == "wolf":
    print("Please go away and stop eating my sheep")
    animal_position = len(animals_list) - animals_list.index("wolf") - 1
    print(f"Oi! Sheep number {animal_position}! You are about to be eaten by a wolf!")