Bonjour a tous.
j'apprends doucement a utiliser ce que j'ai appris et a me faire comprendre par l'interpreteur.
La boucle while a ce stade m'interesse car elle est proche de la langue parlee et du comportement de l'homme quand il calcule "a la main".
Cependant la peinture a la "while" c'est bien difficile meme si ce n'est pas ce qu'il y a de plus" for"!
J'ai un trou dans "while True".
Voici un extrait de wikipedia sur les boucles:
In fact, what you will see a lot of in Python is the following -
As you can see, this compacts the whole thing into a piece of code managed entirely by the while loop. Having True as a condition ensures that the code runs until it's broken by n.strip() equalling 'hello'. The only problem with this is that it slows things down a lot - this is due to first testing the True condition for the while, then again testing the n.strip() value compared to the string 'hello'. Another version you may see of this type of loop uses 1 instead of True - this is exactly the same, though in Python 2.*, using while 1 is minutely faster, due to the ability to reassign True to a different value - the interpretor needs to look up the value of the variable, as opposed to loading a constant.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 while True: n = raw_input("Please enter 'hello':") if n.strip() == 'hello': break
As a programmer, it is up to you which style to use - but always remember that readability is important, and that speed is also important.
Je ne comprends pas ce que veut dire "while True", c'est quoi qui doit être vrai?
Ici on voit l'utilisation de while / if au lieu de while / else
Partager