I WILL LOVE YOU WHILE [condition]:

--Originally published at Coding The Future

Image via GIPHY

I know. I've gotta stop with the song references. But today's topic reminds me of Whitney Houston's "Love You". That song is so beautiful, so inspiring, but it's all FALSE! Well, kind of... Why? Because nothing lasts forever. Well, maybe true love does. But in code, the chances you'll encounter a loop that never ends are super rare.

Today we'll be talking about while loops, which helps us run a piece of code while a certain condition is met.

For example, let's say we wanted to print a countdown. Instead of writing a single line for each number, we would just go to while loops!

While loops are super simple in Python. Begin with the keyword while and then write the condition –without brackets! The following lines, which are obviously indented, is where you put the code that'll loop. Take a look:

count = 0
while (count > 0): print ('The count is:' + count) count = count - 1

As you can see, the loop will run while count is grater than 0, and will stop running once count reaches 0. The count is decreased by 1

For your condition, you can use counters like the example above, or you could use a boolean value by nesting if-statements inside of your while loop. You've gotta be careful though, because as I said, you could get stuck into an infinite loop, which could break your program.

That's pretty much it for today. Until next time!
@emamex98

You will enjoy WHILE you are here.

--Originally published at Hackerman's house

The while loop is a function that repeats a section of the code. This section will be repeated as long as a condition is repeated.

The syntax used is:

while condition:

Section of the code that will be repeated.

The code I created is used to count from 0 seconds to 15 seconds, I also import a python library in a pretty basic way, but we will see that mastery topic in depth in another blog post. As long as the variable in the program is less than 15, the variable will be printed with a 1 second delay. When the condition no longer is fulfilled you can do that the program does something else with the ELSE function.

While loop

Here is how the actual programm works.

While loop result

And this is the final result.

While loop result

I hope this blog was useful for you.

giphy (4)


Prime numbers

--Originally published at Migue´s Blog

A prime number, or also called prime, is a positive integer greater than 1, that number has no positive integer divisors other than 1 and itself, it means that it can’t be factored.Prime numbers.

This is a list of the first prime numbers.

numeros-primos

Yesterday I made a program to know if a number is prime.

To make this program I saw the tutorials located in the page sololearn.com.  This page is free and in this page you can complete tutorials to learn about how to program in phtyon, c++ and java.

Captura de pantalla de 2016-08-21 23-43-33

In my program I used while loops and if conditionals.

The first while loop, while z==1Captura de pantalla de 2016-08-22 10-44-44, is used to make an infinite program until the user press the combination ctrl+c that cancels the infinite loop and closes the program, or you can click on the “X” in your window Captura de pantalla de 2016-08-22 10-42-16.

This line,Captura de pantalla de 2016-08-22 10-46-56 prints the instructions to close the program in the screen for the users that don’t know how to cancel a loop in the terminal.

Captura de pantalla de 2016-08-22 10-50-52In the fourth line with the function “input” the user introduce  a value with the keyboard and the function “int”, integer, converts the value that the user introduced to an integer value that python can use to do mathematical operations, and the value is saved on the variable named “x”

In the fifth line the variable “a” gets a value of 2. This variable will help us to make the next while loop and to finish it when we reach a result for the number.

Captura de pantalla de 2016-08-22 11-11-15This lines exclude the numbers 0, 1 and the negative numbers from the loop because they are not prime numbers and if they enter to the loop can cause some problems when the program is evaluating the number that we introduced.

Captura de pantalla de 2016-08-22 11-04-51In this final while

Captura de pantalla de 2016-08-25 10-16-14
Captura de pantalla de 2016-08-21 23-45-12
Continue reading "Prime numbers"