Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2023 May 6
- # WEBINAR: For Loops
- # We use loops to repeat actions
- # a WHILE loop... is an IF that repeats as long as the loop condition remains True
- # FOR LOOPS are used for repeating actions for every element
- # in a container like a list, string, tuple, etc...
- # Basic syntax of a for loop
- # for __someVar__ in __someContainer__:
- # list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item)
- # strings
- myString = "It was the best of times."
- # for char in myString:
- # print(char, end="-")
- # print() # if you ever override end, make sure to get the "normal" line return back
- # print("This should start on a clean line by itself.") # check yourself! Delete or comment out later
- # for char in myString:
- # if not char.isspace():
- # print(char)
- # tuples
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- # dictionaries
- # myDict = {"key": "value"}
- # myDict[key] # retrieves the value for the key
- # myDict[key] = value # assign a (new) value to a key
- bestOfXF = {
- "1x00": "Pilot",
- "2x10": "Red Museum",
- "2x14": "Die Hand Die Verletzt",
- "3x04": "Clyde Bruckman's Final Repose",
- "3x12": "War of the Coprophages",
- "3x20": "Jose Chung's From Outer Space",
- "4x05": "The Field Where I Died",
- "5x05": "The Post Modern Prometheus",
- "5x17": "All Souls"
- }
- for key in bestOfXF: # what does the loop var hold?
- # value for that key... bestOfXF[key]
- # value = bestOfXF[key] # you could assign a var for that if you wanted to
- print("Check out Episode {} or '{}'".format(key, bestOfXF[key]))
- # range()
- for num in range(0, 5): # [0, 1, 2, 3, 4]
- print(num)
- # Interested in the index and the item/value in a list???
- myList.append("Queequeg")
- myList.append("Krycek")
- for i in range(0, len(myList)):
- print("{} - {}".format(i, myList[i]))
- # you can use enumerate()
- for i, item in enumerate(myList):
- print("{} > {}".format(i, item))
- # Student Questions
- # # question on reversing (not really about For Loops...)
- # myStr = "Hello"
- # revStr = myStr[::-1] # myStr[start:stop:step]
- # Lab 33.10
- ''' Type your code here. '''
- user_input = input()
- quitCommands = ["Done", "done", "d"] # I like to make a list of sentinel values if there's more than 1
- # while user_input != "quit": # if there was only one quit command for a sentinel value
- # check individually as separate conditions...
- # while user_input != "Done" and user_input != "done" and user_input != "d":
- # or...
- while not user_input in quitCommands:
- # do our stuff
- print(user_input[::-1])
- # at the end, get the next input for the loop's next iteration
- user_input = input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement