My string gives line breaks without I wanting it

0
TINTA=[''' 
  _
|---|
|---|
|---|
|---|
|---|
|---|
 \ /
  ' ''']
TINTA2=['''
  _
|   |
|---|
|---|
|---|
|---|
|---|
 \ /
  ' ''']
TINTA3=['''
  _
|   |
|   |
|---|
|---|
|---|
|---|
 \ /
  ' ''']
TINTA4=['''
   _
 |   |
 |   |
 |   |
 |---|
 |---|
 |---|
  \ /
  ' ''']
TINTA5=['''
  _
|   |
|   |
|   |
|   |
|---|
|---|
 \ /
  ' ''']
TINTA6=['''
  _
|   |
|   |
|   |
|   |
|   |
|---|
 \ /
  ' ''']
TINTA7=['''
 _
|   |
|   |
|   |
|   |
|   |
|   |
 \ /
 ' ''']

print("Introduce el texto para calcular la tinta que has usado: ")
 texto=input()

if len(texto)<40:
  print(TINTA)
elif len(texto) >= 40:
  print(TINTA2) 
elif len(texto) >= 60:
  print(TINTA3) 
elif len(texto) >= 80:
  print(TINTA4) 
elif len(texto) >= 100:
  print(TINTA5) 
elif len(texto) >= 120:
  print(TINTA6) 
elif len(texto) >= 140:
  print(TINTA7) 

When I print I get the following:

Programa de cálculo de tinta...
Introduce el texto para calcular la tinta que has usado: python
[" \n\t  _\n\t|---|\n\t|---|\n\t|---|\n\t|---|\n\t|---|\n\t|---|\n\t \  /\n\t  ' "]
    
asked by ripense 16.11.2018 в 19:16
source

1 answer

2

The problem is that each of the variables that you have defined, for example, is:

TINTA=[''' 
  _
|---|
|---|
|---|
|---|
|---|
|---|
 \ /
  ' ''']

is not a string, but a list, due to the square brackets that enclose it. Of course, it is a list with a single element, which does not make much sense.

You can remove the brackets that you have put in all these definitions, and then print(TINTA) will work as expected.

You can also leave the brackets, but in that case print(TINTA) will print the list, with the result you see, and what you want is to print only the first element, for which you should use print(TINTA[0]) (and so on the remaining cases).

On the other hand, the logic of if is wrong, because if the number of characters is less than 40 it will show TINTA , while if it is greater than or equal to 40 it will always show TINTA2 , regardless of since it is greater or less than 60, 80, 100, etc.

I understand that it should be like this:

if len(texto) < 40:
  print(TINTA)
elif len(texto) < 60:
  print(TINTA2)
elif len(texto) < 80:
  print(TINTA3)
elif len(texto) < 100:
  print(TINTA4)
elif len(texto) < 120:
  print(TINTA5)
elif len(texto) < 140:
  print(TINTA6)
else:
  print(TINTA7)

Suggestion for improvement

All these variables TINTA , TINTA2 , TINTA3 , etc. they are asking you to convert them into a single one, called TINTA , which is a list with several elements, each element being each one of the chains. So:

TINTA=['''
  _
|---|
|---|
|---|
|---|
|---|
|---|
 \ /
  ' ''',
'''
  _
|   |
|---|
|---|
|---|
|---|
|---|
 \ /
  ' ''',
'''
  _
|   |
|   |
|---|
|---|
|---|
|---|
 \ /
  ' ''',
'''
  _
|   |
|   |
|   |
|---|
|---|
|---|
 \ /
  ' ''',
 '''
  _
|   |
|   |
|   |
|   |
|---|
|---|
 \ /
  ' ''',
 '''
  _
|   |
|   |
|   |
|   |
|   |
|---|
 \ /
  ' ''',
 '''
  _
|   |
|   |
|   |
|   |
|   |
|   |
 \ /
  ' ''']

This is a list with 7 elements, and to access each of them we would use TINTA[0] , TINTA[1] , etc. up to TINTA[6] which would be the last one.

Now, to choose which of the elements of the painting array, it is enough to make some calculations, instead of the conditional ones:

print("Introduce el texto para calcular la tinta que has usado: ")
texto=input()
longitud = len(texto)
elemento = min(max(0, (longitud-20)//20), len(TINTA)-1)
print(TINTA[elemento])

The element of the array to show is obtained by subtracting 20 from the length and then making the entire division by 20. You can check, for example, that for longitud=30 that would come (30-20)//20 = 10//20 = 0 . For longitud=45 would come (45-20)//20 = 25//20 = 1 , etc.

In general, it works for any length, except for those under 20 that would be negative, or for very large ones, which could be greater than 6 , and therefore outside the limits of the list TINTA . That is why I use the functions min() and max() , to ensure that the result is always between 0 and 6.

    
answered by 16.11.2018 в 19:30