#basic loop """ n = 5 while n > 0: print n n = n - 1 print "Blastoff!!!" #infinite loop w/ break n = 5 while n > 0: print "Infinite" print "loop" break #zero trip loop n = 0 while n >0: print "blah" print "zero trip loop" #breaking out of a oop while True: line = raw_input("> ") if line == "done": break print line print "Done" #finishing an iteration with continue # continue moves computer back up to the top of loop for next iterations while True: line = raw_input('>') if line[0] == '#': continue if line == "done": break print line print "Done" #While loops are called "indefinte loops" because they keep running until a #logical condition becaomes False #definite loops = runs loop once for each of the items in a set # uses "for" and are called definite loop because they execute an exact # number of times for i in [5, 4, 3, 2, 1]: print i print 'Blastoff!!' friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends: print "Happy New Year: ", friend print "Done" loop idioms loops have a high level view of what we want them to do print 'before' for thing in [9, 41, 12, 3, 74, 15]: print thing print 'After' largest_so_far = -1 print 'Before', largest_so_far for the_number in [9, 41, 12, 3, 74, 15]: if the_number > largest_so_far: largest_so_far = the_number print largest_so_far, the_number print 'After', largest_so_far zork = 0 print "Before ", zork for thing in [9, 41, 12, 3, 74, 15]: zork = zork + 1 print zork, thing print "After, ", zork #summing in a loop n = 0 print 'Before ', n for item in [9, 41, 12, 3, 74, 15]: n = n + item print n, "increment value ", item print 'After ', n count = 0.0 sum = 0.0 print 'before ', count, sum for value in [9, 41, 12, 3, 74, 15]: count = count + 1 sum = sum + value print count, sum, value print 'After ', count, sum, sum / count #filtering a loop print 'Before' for value in [9, 41, 12, 3, 74, 15]: if value > 20: print 'Large number', value print 'After' #search using a boolean variable found = False print 'Before ', found for value in [9, 41, 12, 3, 74, 15]: if value == 3: found = True break print found, value print 'After ', found smallest = None print "Before" for value in [9, 41, 12, 3, 74, 15]: if smallest is None: smallest = value elif value < smallest: smallest = value print smallest, value print "After ", smallest """ largest = None smallest = None while True: num = raw_input("Enter a Number: ") if num == "done": print "Done" break else: try: int(num) == True except: print "Invalid input" continue print num for the_number in num: if largest is None: largest = the_number elif the_number > largest: largest = the_number for the_number in num: if smallest is None: smallest = the_number elif the_number < smallest: smallest = the_number print "Maximum", largest print "Minimum", smallest
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question