Difference between revisions of "How to use while & for loops"
From CompSciWiki
m (changed bullets from <li> to * for consistnecy with other wiki pages) |
|||
Line 15: | Line 15: | ||
== Sample programs== | == Sample programs== | ||
− | + | * for loop example | |
{{CodeBlock | {{CodeBlock | ||
|Code=############################### | |Code=############################### | ||
Line 26: | Line 26: | ||
}} | }} | ||
− | + | * while loop example | |
{{CodeBlock | {{CodeBlock | ||
|Code=################################################### | |Code=################################################### |
Revision as of 21:25, 4 April 2012
COMP 1010 Home > Back to Extra Labs
IntroductionIn this program, you will be learn how to use while and for loop control structures in Python. while and for loops syntax in Python:while (expression): Statement(s) for iterating_var in sequence: Statement(s) The two sample programs shown below does the following shows |
---|
Sample programs
- for loop example
############################### #Prints out all lower case alphabets letter = 'a' for i in range (0,26): print letter letter = chr(ord(letter) + 1)
- while loop example
################################################### # This program ensures that the user enters either of the # characters (a, b or c) ## Gets input from th user message = raw_input("Please enter a, b, or c: ") while(message != "a" and message != "b" and message != "c"): message = raw_input("Please enter a, b, or c:") print "You entered: " + message