How to use TAB or ENTER when using raw_input? Python

0

Hi, I would like to know how to use ENTER and Tab when telling the user to enter something with raw_input

Code:

    #!/usr/bin/env python
# -*- coding: utf-8 -*-

outfile = open('codehero.txt', 'w') # Indicamos el valor 'w'.
outfile.write(raw_input("Ingresa lo que deseas agregar : "))
outfile.close()
# Leemos el contenido para comprobar que ha sobreescrito el contenido.
infile = open('codehero.txt', 'r')
print("""

          Este archivo fue modificado y leido correctamente""")
print(infile.read())
# Cerramos el fichero.
infile.close()

So that at the moment you are typing you will allow ENTER and Tab

    
asked by Snoop13 09.10.2016 в 07:27
source

1 answer

1

Good morning reading I found some examples and then playing with them culminates with this function:

#!/usr/bin/env python3
import sys

def leer(mensaje):
    print(mensaje)
    entrada = ""
    while True:
        caracter = sys.stdin.read(1)
        if caracter == '\t':
            break
        entrada += caracter
    return entrada

As you can see, what you do is read from the input character by character in search of a tabulation and when you come across the tabulation character, the concatenation of these returns.

  

It should be noted that the execution remains in the same way pending until   the user presses enter, but only if a tab is found   will come out of the loop.

your code just change the raw_input by the read method:

# -*- coding: utf-8 -*-
import sys

def leer(mensaje):
    print(mensaje)
    entrada = ""
    while True:
        caracter = sys.stdin.read(1)
        if caracter == '\t':
            break
        entrada += caracter
    return entrada

outfile = open('codehero.txt', 'w') # Indicamos el valor 'w'.
outfile.write(leer("Ingresa lo que deseas agregar : "))
outfile.close()
# Leemos el contenido para comprobar que ha sobreescrito el contenido.
infile = open('codehero.txt', 'r')
print("\n\tEste archivo fue modificado y leido correctamente")
print(infile.read())
# Cerramos el fichero.
infile.close()

Anyway here are useful links:

answered by 09.10.2016 / 09:58
source