Quiz 09 – Distance

--Originally published at Programming

In this quiz we have to ask the user for the coordinates of two points in the cartesian plane, and then, calculate the distance between them. Let’s see the code:

Captura de pantalla 2017-03-16 a la(s) 10.51.12

First we importh math because we need the square root then. Our function that will do the magic is call distance, and here is where we write the variables. Finally in this part we write what we want it return, that is the formula of distance between two points. You can find more information about this here: Distance Between 2 Points.

Now, the second part is just to ask the user for the inputs like floats. There will be 2x and 2y, one by each point. Finally, we print the result. Original instructions didn’t ask for this step, but I included it because I think it helps to see how it works. You can avoid to follow the instructions correctly if you prefer.

And this is how it runs:

Captura de pantalla 2017-03-16 a la(s) 10.50.56 Hope it helps  ?

 


Functions in Python

--Originally published at Programming

In this post we will:

¡Recycle!

2fazaqyizlltuiie0

Gif from: http://giphy.com/gifs/helpsgood-2FazaqYizLltuIIE0

But not as the usual way. We will use the programm we made in this post: Fun with numbers. We will do the same, but now with functions. First look how it runs:

Captura de pantalla 2017-03-09 a la(s) 10.53.42

It’s the same you already saw before. But now look at the code:

Captura de pantalla 2017-03-09 a la(s) 10.51.10

Let’s explain how it works.

(This video may help you to understand better Python functions: Python Tutorial for Beginners).

The first part as you see, is just declare functions. Thats why you write def and after the name and the variables in brackets. We need it give us the operation, that why we write return and the operation to perform, that we already describe in the post Fun with numbers. The inputs will be the same way, and the thing that changes is the math operations. We are using functions, so we don’t need to perfom it like the traditional way. We just call the function and finally we print it ?


Sum of numbers

--Originally published at Programming

In our program we will ask for a range of numbers, and then prints the sum of the numbers. For example, if you introduce 6 as de lower and 10 as the higher, we will print 40. Because 6+7+8+9+10=40.

captura-de-pantalla-2017-02-27-a-las-11-07-22

First we declarate the low and the high input like int, because they will be integer numbers. Then, we declarate the result like cero to start. We need a counter to stop the loop when we raise the higher number. This is the a and we start like low, because we will start in the lower number that the user introduce. We also declarate new like low, because is the number we will be adding and it also going to star in the lower number. Top will be the number where we stop, but it doesn’t come into the loop, that’s the reason why we sum 1, to avoid stop 1 number before we want.

Inside the loop, all we are doing, is adding, incrementing  the counter a to comparate with the top, and also incrementing new to do the sum.

Finally, we print the result with a sentence. It also remember us the original numbers we introduced.


Pick a number

--Originally published at Programming

The instructions was: Write a program that picks a random integer in the range of 1 to 100.

Then, you will try to guess that number. If you are up the value write something like “too big” and “too small” if you are under.

Here is the code:

captura-de-pantalla-2017-02-21-a-las-06-58-36

First, we import random. Then we declared that num will be the number we will being trying to guess. In that part we write the range (the orange numbers). After that, we print a little description for the user from our programm. Then we declared guess that is the number the user introduce (remeber, put like and int), and a that is our counter of tries.

The program continues to run until the user guesses the integer and at the end we print how many tries he/she made.

And now, let’s play! ?

captura-de-pantalla-2017-02-21-a-las-06-59-43

 


If, elif, else

--Originally published at マルコ

If permite a que un programa ejecute ciertas instrucciones solo cuando se cumpla alguna condición. Este significa “Si”. La primera línea contiene la condición a evaluar y esta debe terminar con (:). Si la condición se ejecuta esta será “verdadera” en caso contrario se usará otro comando.

If- else: Esta estructura de control permite que un programa ejecute unas instrucciones cuando se cumple una condición y otras cuando no se cumple la condición. If es “Si”, else “si no”.

If-elif-else: Esta estructura permite encadenar varias condiciones. Elif es una contracción de “else if” y permite que se cumple una tercera condición.

Si no se cumple la primera condición, pero si la segunda, se ejecutara esta.

Si no se cumple ni la primera, ni la segunda condición, se ejecutará la tercera.

ifelse.JPG


Funciones en Python

--Originally published at マルコ

Una función es una parte de código que se puede reutilizar, el cual se encarga de realizar una determinada orden.

Para definir una función en Python se debe poner “def” seguido del nombre de la función, y dentro de los paréntesis deben de ir los parámetros, este debe finalizar con “:”. Al momento de definir los parámetros se debe tomar en cuenta que el primer valor va al primer parámetro y así sucesivamente.

python3.JPG


Uso de Comentarios

--Originally published at マルコ

Los comentarios, en programación, funcionan para explicar a los usuarios, que es lo que hace el programa, así como explicar algunas partes del código.  Estos comentarios son ignorados al momento de ejecutar el programa. Son utilices al momento de realizar un programa, ya que, puedes hacer notas de cómo funciona el programa o aclaraciones, respecto al mismo.

En pyhton los comentarios se pueden poner en dos formas:

Utilizando el símbolo “#” delante de la línea del texto, donde se escribe el comentario

Utilizando triple comilla “”” al principio y al final del comentario, el cual puede ocupar más de una línea.Comentarios.JPG


WSQ01

--Originally published at Tomas Enciso

Never in my life have a written a blog post so here it goes.

For the first post im gonna show a program I had to write that basically is a simple calculator and asks for your name, using inputs, outputs and some other fun stuff.

first of I started by printing “please enter your name” followed by an input that allows the user to enter his or her name. After that, this person will be greeted by hello (and the user’s name).

Using variables I was able to let the user choose two numbers and you would get the sum, subtraction, multiplication, the division with no decimals and the residue or the decimals of the previous division.

This is the code that I wrote:

print(“please enter your name”);
name = input(“”)
print(“Hello”,name)
x = int(input(“Please enter a number “))
y = int(input(“Now enter a different number “))
print(“If you sum these two numbers you get “,x + y)
print(“The subtraction of this two numbers is “,x – y)
print(“The multiplication of “, x, “and”, y, “is”,x * y)
print(“If you really want to know the division between”, x , “and”, y , “its”, x//y , “I guess…”) # no decimals included
print(“The remainder of this division is “, x % y, “im done, please make it stop”)

and here’s when you run it:

Tomass-MacBook-Pro-2:tecgda TomasEnciso$ python3 WSQ01.py

please enter your name

Tomas

Hello Tomas

Please enter a number 10

Now enter a different number 5

If you sum these two numbers you get  15

The subtraction of this two numbers is  5

The multiplication of  10 and 5 is 50

If you really want to know the division between 10 and 5 its 2 I guess…

The remainder of this division is  0 im done, please make it

Continue reading "WSQ01"