sourcetaya.blogg.se

Python while loop
Python while loop









python while loop

If you keep all the above in mind, you can easily define the for loop as follows:Ī for loop is a programming concept that, when it's implemented, executes a piece of code over and over again "for" a certain number of times, based on a sequence. As you probably would have expected, the "for" component in "for loop" refers to something that you do for a certain number of times. You can tackle the for loop in the same way as the while loop. # As long as `n` is not equal to `1`, run `collatz()` # If the mod of 2 isn't equal to 0, print `3 * number + 1` Note that this is actually an implementation of collatz sequence, which in short is a mathematical problem where you choose a number and you keep doing the above calculations until you arrive at a result of 1.īelow, you can find the solution: def collatz(number):

python while loop

The program should keep calling the function collatz() on the number until it returns a 1. If number is odd, then collatz() should print and return 3 * number + 1. If number is even, then it should print the result of number/2. Write a function collatz() which lets the user input an integer in a variable named number. Print("The number "+str(number)+" is odd") Print("The number "+str(number)+" is even") Take a look at the following example: # Take user input The above example is a bit basic, you can also include conditionals, or, in other words, an if condition, to make it even more customized.

python while loop

In this case, there isn't any more code so your program will stop. Because the condition now evaluates to False, you will exit the while loop and continue your program if it contains any more code. You print out "Thank you" two more times before the value of number is equal to 5 and the condition doesn't evaluate to True any more. While the value in number stays smaller than 5, you continue to execute the two lines of code that are contained within the while loop: "Thank you" Since the value in number is smaller than 5, you print out "Thank you" and increase the value of number with one. If you go into detail in the above code, you see that there is a variable number in which you store an integer 2. The code example above is a very simple while loop: if you think about it, the three components about which you read before are all present: the while keyword, followed by a condition that translates to either True or False ( number < 5) and a block of code that you want to execute repeatedly: print("Thank you") # Increment the value of the variable "number by 1" Now that you know what you need to construct a while loop, all that is left to do now is to look at a real-life example where the while loop is used before you start making exercises on your own! Consider the following example: # Take user input That's all it takes! How To Make A While Loop in Python

python while loop

  • A block of code that you want to execute repeatedly.
  • A condition that transates to either True or False And.
  • The above definition also highlights the three components that you need to construct the while loop in Python: With all of this in mind, you can easily understand the following definition of the while loop:Ī while loop is a programming concept that, when it's implemented, executes a piece of code over and over again while a given condition still holds true. As you already know by now, the word "loop" refers to a piece of code that you execute repeatedly. It is arguably also one of the most intuitive ones to understand: if you think of the name of this loop, you will quickly understand that the word "while" has got to do something with "interval" or a "period of time". The while loop is one of the first loops that you'll probably encounter when you're starting to learn how to program.











    Python while loop