Word repetition

Asked

Viewed 235 times

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.

  • Not to mention that the answer to the other question is just a block of code, there is no explanation or information in it.

  • Cadu, that reply shows how to do using repeating structures; the function there is just one more organization.

  • 2

    As there explicitly asks to be recursive, I decided to open the question.

  • 1

    " ".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 Anderson already did, but I appreciate if you do, because it will be another way to learn how to do.

Show 1 more comment

1 answer

4


An object of the type string, in Python, allows it to be multiplied by an integer generating a new string being the first repeated several times. See:

>>> 'incomodam ' * 3
incomodam incomodam incomodam 

So when the value is even, just add this string in the text:

if i % 2 == 0 and i > 1:
    print("{} elefantes {}muito mais.".format(i, 'incomodam ' * (i//2)))

In this way, your code will produce:

Digite a quantidade de elefantes: 7
1 elefante incomoda muita gente.
2 elefantes incomodam muito mais.
3 elefantes incomodam muita gente.
4 elefantes incomodam incomodam muito mais.
5 elefantes incomodam muita gente.
6 elefantes incomodam incomodam incomodam muito mais.

Notice that I even used i // 2 and not just i / 2; that’s because the division i/2 will return a float, while i//2 will return an integer. A string allows it to be multiplied by an integer, not by a float. The same result would be obtained by multiplying by int(i/2).

It is also important to observe the parentheses: 'incomodam ' * (i//2). They will serve to define as a priority the division operation and not the multiplication one. Because they naturally have the same priority, the interpreter would analyze the expression from left to right, performing first multiplication and then division. In this case, you would have 'incomodam incomodam incomodam ' // 2, which doesn’t make any sense (even would make a mistake saying Unsupported operand type(s) for /: str and 'int').

Also remember that range(1, n) will generate the whole [1, n-1], so when informed 7 elephants, the output only reaches 6. This will make less sense even when the input is 1, because there will be no outputs in the program. It might be interesting to correct for range(1, a+1).

  • That / the more makes all the difference. I had done what you said, but only with a / , then gave that error that it is not possible to multiply a string by a float. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.