Validated user input (ensure correct/expected data entry)

--Originally published at Py(t)hon

Most of the time we want our program to be able to interact with the user, to ask him questions and him returning input, well, it is very important to validate the input the user is giving so our program doesn’t crash. For example we have a program were the user has to input a number, how we validate that that input will be indeed a number, we can use the try…except….else block, here is how:

valid-1

That’s all #Pug#Valid#Input#ISC#Tec#TC101#NoMore

 


TEACH ME HOW TO BOT – Building a simple Twitter Bot

--Originally published at Coding The Future

Greetings everyone! I am back, and although this is long overdue, it's finally here!

Today I am releasing my tutorial on how to build a simple Twitter Bot. I have been working on a bot called Manny's Fortune Ball, a simple bot that replies with future predictions every time you ask it a question. You can go check it out by visiting its Twitter profile, and asking him a question. Its handle is @askemanuel.

Above I have attached a interactive slideshow. To move on to the next slide, just click on it, or tap the arrows.

Finally, this is the link to the GitHub repository so that you can check my source code: https://github.com/emamex98/TwitterFortuneBall

I hope you enjoy the tutorial! If you have any questions, do not hesitate to contact me:
Twitter: @emamex98
Email: em (at) nuel.xyz

comments powered by Disqus

Foolproof.

--Originally published at Hackerman's house

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.

try except loop.JPG

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.

try-except

Thanks for reading me.

Here is some nice information about this process. http://www.101computing.net/number-only/

giphy


YOUR ARGUMENT IS INVALID, isn’t it?

--Originally published at Coding The Future

via GIPHY

If you had been in my house while I was watching the US Presidential Debates you would probably had heard me scream to my computer "YOUR ARGUMENT IS INVALID DONALD!". Not only do I hate that guy for being so racist, but also because everything he says is just so dumb. And then my girl Hillary comes in saying that you can fact-check Donald from her website in real time. Gotta love that QUEEN!

But aside from US politics, let's move on to today's topic: validating user input. This will not exactly help us to fact-check Donald, but to make sure that the user is entering the right kind of information.

Let's take a registration form for example. We want to collect the user's name, email, and phone number. Therefore, we must make sure that the user includes an @ and a .something and that only numbers are inputted in the phone number field. Here's how it works...

Using the "Try" Approach

To validate input, we could use if-statements, but there's a more effective approach commonly known as Try-catch. This method tries to do something, and if it can't, instead of crashing the program, makes it do something else.

Take a look at the following sample code:

try: phoneNum = int(input("Please enter your phone #: ")) except ValueError: print("Sorry, that phone number is not valid.")

As you can see, the first step was to type try followed by the code you need to validate, which in this case is making sure the input only whole numbers, and then using an except statement to break out of if an error was returned. This prevents the program from crashing, and if combined with a while loop, can prompt the user to enter their number again until they get Continue reading "YOUR ARGUMENT IS INVALID, isn’t it?"

DO NOT READ THIS!!

--Originally published at Coding The Future

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.

Opening text files

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.

textFile = open(loremIpsum.txt)

Reading text files

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.

In the following sample code, I read the text file I previously imported and print it:

print(textFile.read())

As you can see, since the text file is already stored in the textFile variable, I use the function within the file object.

Processing files

By processing, Continue reading "DO NOT READ THIS!!"

FORever ALONE

--Originally published at Coding The Future

Image via GIPHY

Today, I will share the very sad story of my relationship life: I am, and forever will be a forever alone. But that's ok. To be honest, I don't mind the single life. Love's just complicated for me.

But anyways, you are probably wondering why am I telling you this, which is probably useless. Well it kind of is useless, but today's topic, For Loops, got me thinking.

So, let's dive into today's topic: For Loops. Remember while loops? Well for loops are not so difference. Only difference is that this time, we know exactly how many times the loop needs to run.

Here's an example: Let's say we wanted to ask a user to enter their password, and we need the user to enter it twice. Since we know that the loop will run exactly twice, the most appropriate way to accomplish this is a for loop. Let's take a look at the following program:

password = ["pass1","pass2"]
i=0;

for counter in range(0,2):
⠀⠀password[counter] = input("Please enter your password: ")
⠀⠀i= i+1

if (password[0] == password[1]):
⠀⠀print("Both passwords match")
else:
⠀⠀print("Passwords do not match")

As you can see, the first two lines simply declare a list and an integer. The following lines is where the magic happens. As you can see, I started with the keyword for, and declared a counter variable called counter, followed by the key words in range and the range from which the loop will run. As you can see, this range is in brackets, and it says (0,2). Check the footnote to know why this means it will run twice.1 I end off with a colon.

The code bellow just asks the user for input, stores it in a list, and at the end of the loop Continue reading "FORever ALONE"

input(“¿Qué es un input?”)

--Originally published at Eduardo's Projectz

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().

captura-de-pantalla-de-2016-09-12-10-14-20

Lo que hace esta función es recibir información de una fuente externa y guardarla en una variable previamente establecida.

captura-de-pantalla-de-2016-09-12-10-14-35

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.

captura-de-pantalla-de-2016-09-12-10-15-03

captura-de-pantalla-de-2016-09-12-10-27-24

 

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().

captura-de-pantalla-de-2016-09-12-10-32-20

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.

 

Para más información visita: https://docs.python.org/3/tutorial/inputoutput.html ; http://www.python-course.eu/input.php ; http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html ; http://www.mclibre.org/consultar/python/lecciones/python_entrada_teclado.html

59943535

 

 

 

 


FOR ALL THOSE PROCRASTINATORS

--Originally published at Coding The Future

If you are reading this right now, you were probably procrastinating, and just started studying for tomorrow's test, right?

Well, as always, I've got you covered. Here's a quick video summary of what you need to know for the first partial. I hope things are not too strange.

Good luck!

Input = not so difficult

--Originally published at Py(t)hon

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.

So this is how it goes:

Something = input ( whatever you want to ask)

Then you can go like:

Print = (“This” + Something + “ is very nice”)

Here is one example:

input1

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.