Python Projects : a simple WHILE loop

Hello World,
 As some of you have noticed, I've recently been learning Python with my buddy "H", we have a specific application in mind that we are trying to create, so we are doing our best to gather as much helpful information as we can while creating mini programs on the way.
I actually bought a journal in which I'm keeping track of helpful information that I come across that would be helpful to our final application. And I will be sharing some of these projects and what I've learned as I go along.

Ok, so one thing that I had some trouble with was loops. The two specific ones are the "FOR" loops and the "WHILE" loops. Since many of the tiny programs that I'm working on sometimes ask the user if they want to try something again, a WHILE loop would be great for such a thing.
So on a program I was working on yesterday, I literally deleted the whole loop area and started form scratch, and I saw how simple it actually is.

So here is the test WHILE loop that I used to make sure the its working properly:
while_answer = 'y'
while (while_answer.lower() == 'y'):
          print("Add your function here")
          while_answer = input("Do it again? y/n")



That's it, just 4 simple lines, and it can do wanders.
This is actually doing 2 things in one:
Thing 1: It allows you to loop the function as many times as the users likes
Thing 2: the "while_answer.lower()" line, converts the user's input into lower case, which means if the user types in 'y' or 'Y' it will accept it and go through the loop. Anything else, it will go out of the loop, its a nice little check for those CAPS LOCK fans out there.


Since I figured this out, I have been able to add menus to my applications and allow the programs to loop as many times as needed, its quite lovely.
I will be posting some of these Python Projects up here for you to see, hopefully they will be helpful.

"Coding is the closest thing we have to a superpower" - anonymous
Go be a Super
-ash

Comments

Popular posts from this blog

No more controllers for playing games

Look out iPhone, the G1 is coming. sw33t.