Author Archives: Nuel

Follow my Twitter account @ManLepe1 and manlepe in GitHub #Mastery3

Follow my Twitter account @ManLepe1 and manlepe in GitHub

Fun With Functions (Define and Call Functions)

This is a little remake of the Fun With Numbers program, only that in this case we will define and call functions to reuse chunks of code.

The preview of the code is this:

This looks a little weird, there are lines missing and what about the lines before the prints?Well the answer is that they have been defined as functions, so you can retract and collapse them so you can have a better look at your code. 

That’s what functions are for you can use chunks of code several times and save you coding and space. 

Sum of Numbers (‘for’ loops)

The next program ask the user for two numbers, the first is the lower limit of a range of numbers, and the second is the maximum. The program then takes all the numbers in the given range and makes the sum of all the numbers in the given interval, including the limits.

The source code is the given:

of Numbers

 

n1=int(input(“Enter the lower limit of the range “))

n2=int(input(“Enter the higher limit of the range “))

 

if(n2<n1):

    print(“The lower limit must be a lower number than the second”)

sumnum=0

 

for num in range(n1,n2+1):

    sumnum=sumnum+num

 

print(“The sum is “,sumnum)

The first thing the program does is validate that the user don’t crash the program, it simply applies a conditional that tells the user that the inputs are incorrect, thus the program wont do nothing.

In the program we can see a for loop. The for loop is used when we know how many times we are going to be in a loop, or when we need a given variable that will be incremented in every run that we make of the loop. This is used very often to navigate inside arrays and other arranges. 

The for loop in Python is used in conjuction with the range command, wich gives us an array of integers when we type the lower and the upper limit; but beware, the upper limit is not inclusive, thus you must use a +1.

Then the program uses this loop and navigates the array adding to the accumulative of the two numbers before the next number in the array, thus making the sum of all the numbers in the array. 

At the end the program prints the accumulative sum of all the integers in the array. 

This is for my of my

Random number (Loops and Modules)

This program calculates a random number, and asks the user to guess it by using the bisection method. This means it tells you if your guess is higher or lower. 

The source code is the following:

Number

from random import randrange

number=randrange(1,101)

counter=0

guess=0

while(guess!=number):

    guess=int(input(“Try to guess a number between 1 and 100 “))

    counter=counter+1

    if(guess<number):

        print(“Higher”)

    elif(guess>number):

        print(“Lower”)

 

print(“Correct “,number)

print(“You did in the try number “,counter)

 

First of all in order to generate the random number we find that there is a module named Random, which description we can find in the Python3 Documentation. So the first thing I did was searching information about Modules and how to import functions of them in my own program.

So the first line is used to import the randrange function from the random Module, wich gives us pseudo random numbers (random numbers are virtually impossible to get) between a  given interval without including the borders, that’s why the 101, and we asign the given number in the variable number.

Then I assigned a variable counter that is adding the number of tries the user has done to guess the number. And then I assigned the guess the value of 0, the reason is to initalize the guess as a number that isn’t in the range of the pseudo number generator.

The next step in the program is the part when the user is making several guesses to say the number the computer generated. And when we have a task that is reppeated without us knowing how many times it will be done we need to use the while loop.

You can find more information about loops in this page. The while loop is used to execute a task while the expression being evaluated proves to be false. Once the condition is proven true the program exits the loop and continues with the code below.

The other addition I did to my program is the elif command. This one is used to nest to ifs together, this means you can evaluate two different expression before an else.

In the end when the user introduces the number that the computer generated it goes out of the loop and print the number that was the answer, and right after it prints the value of the counter used for the number of tries.

This is for my of my .

 

Temperature (Use of conditionals)

This program is intented to be an intruction to conditionals statements if and else. 

The program consist in asking the Temperature in Farenheit to the user, transform it into Celcius, print it in the screen and then letting the user know if water would be boiling or not. 

The source code is this:

temp=float(input(“What is the temperature in Fahrenheit? “))

 

temp=5*(temp-32)/9

print(temp,”ºC”)

 

if(temp<100):

    print(“Water is not boiling”)

else:

    print(“Water is boiling”)

Where we can see how we ask for the temperature to the user and then store it in the variable temp, wich in this case I have declared it as a float, just to get decimals nothing more. 

Then we apply the formula to convert Farenheit into Celcius and store it in the same variable we have created before. If we wanted to recieve an integer answer we could use a floor division // instead of the /.

Then comes the conditionals, the if statement evaluates the expression inside the parenthesis and if the expression returns a true value it execute the code below. The else statement is used to have an alternative when the if statemen expresion returns a false, if it does it executes the block of code below that has the appropiate identation.

Here you can have more information and examples to the use of conditional statements.

This is for my of my course.

About Me

I’m a Mecatronic Engineer and I’m very proud about my choice. I liked playing video games since I was a boy and I have always wanted to build things. The science fiction and my hunger for knowledge led me to Mecatronics. 

I have always wanted to build disrupting technolgies, and many things that may change the way the world is nowadays. I’m a Tolkien and a Nietzche fan, and I believe that man can accomplish great things and that it lacks the focus and the determation to do them.

I believe money is a distractor to accomplish greatness, and that man should understand that money is not the final goal but a mean to a goal. 

Fun With Numbers

Program that uses several mathematical functions on to integers asked to the user. 

Flipped Learning

The Flipped Learning is a teaching technique that makes the students involve in the subjects they are going to study in the classroom.

Why is it better? Because this way the students can make a better use of the teachers knowledge and experience, the students understand better the subject and they have questions they have already tried to solve, so the teacher teaches the very thing students need to be teached. This helps a lot for saving time and effort of both the teachers and the students. 

This is my for my .

Creating and Executing Python Files in OS X

Although we can type Python commands directly to the terminal, in almost any programming lenguage the best approach is to use a text editor to type your code and, in the case of Python that is a translated language, run it.

I will use Atom. It a text editor developed by GitHub, that I have used with Python, C, C++ and Java and it’s very friendly and I don’t think it spoils you that much. 

 

Installing Python 3 on Macbook Pro (2010) Yosemite

The first thing to do installing any programming language is to look for its official Site. Here is the Python Official Site, where we will arrive at its Home Page that looks like this:

As you can see there are a lot of options and resources for you to explore. In our case we need to click or hover over the Downloads tab. The page automatically identifies your OS, so it will give you the option to install both Python 3.X.X or the Python 2.X.X

And we click in the desired version of Python we want to download. 

Whatever your browser is the dowload should start the moment you click. The Download is a .pkg and once it has finished downloading you should have this in your downloads folder.

Once the download has completed, you open the .pkg file and you should see the installer 

Once you have read the Introduction, the Read Me and read and agreed with the Python License, you will be prompted to choose the installation location of Python. You can leave the default it won’t break your computer. And you click the Install button.

You will be then asked for your computers password for security reasons. And then the installation should begin. If you get this screen then you are all set.

 

For the future of my posts I will be using the terminal, not the IDE that has been installed. But since we have allready installed the pakage you now have the commands in your terminal.

So for being sure everything its ok, we are going to open the terminal and type:

 

python3

And if everything is set you should get this:

If you have the same in your terminal it means that now you are using Python3 to write codes. You can now try some commands and have a little fun with Python.

I highly recommend the you use the Python 3 Documentationso you can try different commands and star learning how to do more things with Python 3.

 I did all of these for mi .