It is not very clear whether you just want to display everything on the same line or store, in fact, in the same list, since you are overwriting the value of binario
at each iteration.
As, in my view, it would not make much sense to just display on the same line and store separately, I will consider that you also want to store on the same list. To this end, considering that the return of dec_to_bin
is a string, which is converted to a list with list(...)
, then you can do:
binario = []
for n in receivedMessage:
contador += 1
print (contador)
binario += list(dec_to_bin(n))
print(binario)
Realize that the print(binario)
is outside the loop to print only the final value. The changes that modify the logic are: 1. set initially binario
as an empty list and 2. within the loop only increment its value using the operator +=
.
This way, the result will look like:
binario = ['1', '1', '0', '0', '0','0', '1' ,'1', '1', ...]
What is the function
dec_to_bin
? What are the values ofn
? What is the result that appears when you execute the code? Finally, what would be the desired result?– Woss
The dec_to_bin function converts the decimal value of each character of a string, for example the decimal values of each letter of this "aeiou" phrase to its binary value. when I run the print appears
– Sergio Nunes
The dec_to_bin function converts the decimal value of each character of a string, for example the decimal values of each letter of this phrase "aeiou" to its binary value, when I print appears Rx_binario: ['1', '1', '0', '0', '0','0', '1'] to the letter "a" in the following line Rx_binario: ['1', '1', '0', '0', '1', '0', '1'] for the letter "and" and I wanted to put it all in the same line and not in separate lines, so Rx_binario ['1', '1', '0', '0', '0','0', '1', '1', '0', '0', '1', '0', '0', '1'']
– Sergio Nunes
Information relating to the question should be added to the question with the [Edit].
– Woss
He did not let me edit again because I made a second comment with the answer to the question
– Sergio Nunes