It is usual that the programms that we use in our daily life require the interaction of the people, in this case there are occasions that an input is required to be given by the user, but how can the program now that the type of information the user gives can be used to perform a certain action.
A common case is when the program needs an integer to perform a mathematical operation but the user gives a string. In this case we can use a simple function. It is the try/except.
In this program, if a number is given it will be stored as the user input. But if the data given is from other type like a string, it will ask again, and again. This loop will make sure that the user gives the correct data.
Original image by Romain Vignes.
Hello! Ok, I must begging by saying that reading this article will not kill you nor hurt you in any way. In fact, it will be beneficial! So why the title, you may ask? Just to catch your attention! ?
Today's topic is reading and writing text files. This is one of the most important aspects in programming, because if you think about it, 99% of things online are text-based. Think about it... although Google has the option to search through voice and image, 99.9% of the times you probably just type what you need to find and hit enter. See? This is why programs must be able to interpret text because we live in a text-based world.
I want to get things clear though... When I say text files, I am not referring to MS Word documents –those are processed word docs– instead of plain .txt files. Let's get going.
Let's begging by looking at how to open a text file, which is the first step to reading or writing on the file.
In order to do this, we will need to declare a variable where a text file object will be stored. To do so, use the open( ) method and pass the filename as a parameter.
Once we have the text stored in textFile, our sample variable, we can read it to process its contents. To do so, we need to use the .read() function.
As you can see, since the text file is already stored in the textFile variable, I use the function within the file object.
Como la mayoría de ustedes deberían saber, existe una forma para hacer que un individuo externo pueda modificar el valor de una variable.
Esto se puede lograr al llamar a la función input().
Lo que hace esta función es recibir información de una fuente externa y guardarla en una variable previamente establecida.
Esto guarda la información recibida en la variable nombre.
Dentro de los paréntesis se puede escribir un output. Lo que hace es que se mostrara un output al usuario para que este pueda saber mejor que es lo que tiene que ingresar.
Si te interesa que la información ingresada sea guardada como cualquier otro tipo de variable que no sea un string debes de hacer explicito que es lo que necesitas.
En caso de querer que la información ingresada sea una Integer debes de establecer int() antes de input().
En caso de querer otro tipo de variable se hace lo mismo que para un Integer pero cambiando el int() por el tipo de variable deseada: float(), long(), hex(), etc.
Hi again, today we are going to see how to make an input, it is actually not that hard so i think you won’t have any difficulties.
Inputs are not difficult so don’t waste a lot of time with them, focus on more important things, but if you have difficulties you can always ask Ken.