1
I’m developing an algorithm that sings the elephant’s music, I think everyone knows it. Who doesn’t, it works like this:
a) For the first elephant, write in the singular.
b) For the second elephant on write elephants.
c) Whenever the quantity of elephants is odd write: X elephants bother a lot of people! , where X is the amount of elephants.
d) When the number of elephants is even, repeat the word are bothered by half the amount of elephants example, 6 elephants bother, bother, bother much more!.
There is already here in Sopt the following question that addresses the same subject: Recursive function with strings - Python. However I do not want to use functions to solve this problem, because as I am learning the language still, I understand that I need to fill some gaps in my knowledge before starting to use functions.
The code is here:
a = int(input("Digite a quantidade de elefantes: "))
for i in range(1,a):
if i == 1:
print("{} elefante incomoda muita gente.".format(i))
if i % 2 == 0 and i > 1:
print("{} elefantes incomodam muito mais.".format(i))
if i % 2 != 0 and i > 1:
print("{} elefantes incomodam muita gente.".format(i))
My question is this: As you can see, the code I wrote is not that part of the problem:
repeat the word bother by half the amount of existing elephants
The question is this: What function can I use to do this repetition?
I tried to concatenate, but it didn’t work. I tried the function join()
, but I confess that I did not understand very well the syntax when I read the documentation.
@Anderson Carlos Woss I still do not know how to use functions, so I disregarded the question you marked as duplicate. I want to do without using functions.
– Cadu
Not to mention that the answer to the other question is just a block of code, there is no explanation or information in it.
– fernandosavio
Cadu, that reply shows how to do using repeating structures; the function there is just one more organization.
– Woss
As there explicitly asks to be recursive, I decided to open the question.
– Woss
" ".join(["incomodam"] * num)
does what you want. I formulate a decent answer with better explanation in the afternoon (if someone doesn’t do it before)– fernandosavio
@fernandosavio Anderson already did, but I appreciate if you do, because it will be another way to learn how to do.
– Cadu